* Erdogan seen eyeing presidency after strong local polls
* AK Party took around 45.6 pct of national vote
* Elections marred by corruption scandal, leaks
* Markets rally on hope of near-term stability (Adds officials' comments, market reaction, edits)
By Humeyra Pamuk and Nick Tattersall
ANKARA, March 31 (Reuters) - Turkish Prime Minister Tayyip Erdogan looked a step closer to a presidential bid and to gaining the upper hand in a bitter power struggle on Monday, casting strong local election results as a mandate to hunt down enemies within the state "in their lair".
His AK Party swept the electoral map in Sunday's polls, retaining control of the two biggest cities Istanbul and Ankara and increasing its share of the national vote as his pugnacious leadership style, beloved by a loyal, conservative voter base, trumped a stream of corruption allegations and security leaks.
From a balcony at AKP headquarters at the end of a long and bitter election that became a referendum on his rule, Erdogan told thousands of cheering supporters that his enemies in politics and the state, whom he has labeled "traitors", "terrorists" and "an alliance of evil", would pay the price.
"We will enter their lair," he said, before a huge firework display lit up Ankara's midnight sky. "They will be brought to account. How can you threaten national security?"
The harsh tone of his balcony address suggested he felt he now had a mandate for strong action against his enemies. "From tomorrow, there may be some who flee," he said.
The election campaign has been dominated by a power struggle between Erdogan and U.S.-based cleric Fethullah Gulen, whom he accuses of using a network of followers in the police and judiciary to fabricate graft smears in an effort to topple him.
Erdogan, who has long drawn support from the same Muslim professional class that reveres Gulen, has purged thousands of police officers and hundreds of judges and prosecutors since anti-graft raids in December targeted businessmen close to the premier as well as the sons of some government ministers.
Investors, who have been unnerved by the turbulence, took solace in the election result, seeing it as a sign of political continuity. The lira rallied to its strongest in two months and stocks hit a 3-month high.
"From a market perspective, the election result appears to be more or less what the doctor ordered: a solid win for the AKP which shores up the position of Turkey's ruling party," said Nicholas Spiro, head of Spiro Sovereign Strategy.
The main opposition CHP said it would challenge the result in Ankara, which was a particularly close race. But no major changes were expected in a nationwide tally which put the AKP on some 45.6 percent with almost all the votes counted, a robust increase on its 39-percent share at local elections in 2009.
Erdogan has made no secret of his ambition to become Turkey's first directly elected president in an August ballot, but the feud with Gulen, the corruption allegations and street protests last summer had all raised questions over how easily he would secure a majority in the first round.
"Of course this has reinforced Erdogan's bid for the presidential polls," one source close to the government said of Sunday's election result. "He was in need of a vote of confidence, both from the people and for those who have been critical of him within the party."
A senior government official concurred, saying there were now "no obstacles before him" on the road to the presidency, although the official said rooting out Gulen's influence within the state would remain Erdogan's priority. He could, instead, choose to run for a fourth term as premier in a parliamentary election next year in order to finish off that battle.
"Erdogan is certainly much closer to the presidency," the official said. "But he makes his own agenda. Very soon he will begin his assessment of what needs to be done together with the party's ruling echelons."
The crisis reached a new level at the end of last week when a recording of a top-secret meeting of security officials about possible intervention in Syria was posted anonymously on YouTube. The action, for which Gulen denies any responsibility, raised serious concern about government control of its own security apparatus and speculation about further damaging leaks.
"The prime minister takes this extremely seriously," the source close to the government said. "Such a structure can not be allowed within the state and before he makes a move for the presidential bid he needs to make sure that these people will be carved out of the state institutions."
"BURIED AT THE BALLOT BOX"
Erdogan, lacking trained personnel loyal to himself, filled government departments with Gulen supporters when he first was elected in 2002. Gulen, who runs a huge network of schools and businesses, is widely credited with having helped Erdogan break the political power of the armed forces using allies in the police and judiciary.
But in recent years friction has grown between the two men and came to a head when Erdogan moved to curb Gulen's influence and close the schools that are a key source of income and influence.
He now seems likely to step up his drive against Gulen.
"Let me tell you, Erdogan's response is coming," said Tesev think-tank chairman Can Paker, seen as close to Erdogan.
"He will harshly and fully clean up the police and judiciary. And he will purge the press that supported the leaks. He will most certainly do that. He will say 'I was elected to eliminate them'. He is not going to soften."
The authorities have already begun an espionage investigation following the leaked Syria recording, and on Monday the Cihan news agency and the Zaman newspaper - both affiliated to Gulen - claimed they had come under "cyber attack" during their election night coverage.
Pro-government newspapers heaped scorn on Erdogan's opponents, with one, Yeni Safak, showing pictures of opposition leaders and Gulen under the headline "Buried at the Ballot Box."
"It's already clear from his speech this evening that he's basically threatening society," said Gursel Tekin, CHP Vice President. "This shows his state of mind isn't to be trusted, and these obvious threats are not something that we can accept." (Additional reporting by Orhan Coskun and Jonny Hogg in Ankara and Ralph Boulton in Istanbul; Writing by Nick Tattersall; Editing by Alastair Macdonald)