* Erdogan declares victory after local polls
* AKP has 45.6 pct, with 98 pct of ballot counted
* Elections marred by corruption scandal, leaks
* Strong performance could prompt presidential bid
* Turkish lira at 2-month high as turbulence seen easing (Adds market reaction, updates results)
By Humeyra Pamuk and Ralph Boulton
ANKARA/ISTANBUL, March 31 (Reuters) - Turkish Prime Minister Tayyip Erdogan declared victory in local polls that had become a referendum on his rule and said he would "enter the lair" of enemies who have accused him of corruption and leaked state secrets. "They will pay for this," he said.
Erdogan, fighting the biggest challenge of his 12-year rule, addressed supporters from a balcony at AKP headquarters at the end of a long and bitter election campaign in which he has labeled his opponents "terrorists" and an "alliance of evil".
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 a moderate U.S.-based cleric, Fethullah Gulen, whom he accuses of using a network of followers in police and judiciary to fabricate graft accusations in an effort to topple him. Erdogan has purged thousands of police and hundreds of judges and prosecutors since anti-graft raids in December targeting businessmen close to him and sons of ministers.
"We will enter their lair," he said. "They will pay the price, they will be brought to account. How can you threaten national security?"
The turbulence has unnerved investors, driving the lira currency to a record low in January and prompting losses in stocks of some 8.6 percent since late last year. The strong AKP showing, signalling political continuity, calmed nerves however on Monday and the lira hit a two-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.
At the end of last week, the crisis reached a new level 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 fears of further damaging leaks.
NATO member Turkey, under Erdogan, was long held up as a model for a Muslim democracy and indeed the prime minister carried out many reforms that eased human rights and drove the economy. But since a crackdown on anti-government protests last June he has been accused of intolerance.
With about 98 percent of votes counted by early on Monday morning, AKP, in power since 2002, had 45.6 percent of the vote, the opposition CHP trailing with 28 percent, according to Turkish television. If borne out, the result would be at the upper end of what Erdogan might have expected, although the race for Ankara was going down to the line.
The CHP, Erdogan said, must look at itself in the mirror.
"The old Turkey is no longer. The new Turkey is here," he said, to cheers from supporters who shouted Allahu Akbar (God is greatest) and "Turkey is proud of you". "Today is the victory day of the new Turkey, 77 million united ... as brothers."
Erdogan, lacking his own trained personnel, 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 him break the army's political power using his people in police and judiciary.
But in recent years friction has grown between the two men and came to a head when Erdogan moved to curb his influence and close the schools that are a key sort of income and influence.
Erdogan seems likely now to step up his drive against the followers of Gulen, who denies any wrongdoing. Criminal investigations and arrests could follow, especially after last Thursday's leak of the meeting between spymaster Hakan Fidan, a close Erdogan confidante, and military and civilian chiefs.
"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."
BLOW TO OPPOSITION
The strong showing could embolden Erdogan to run in what will be the first popular election for the presidency in August. In doing so, he would take over a role that has been largely ceremonial, but with the aim of extending its powers.
There would be some risks involved. Erdogan has described audio recordings anonymously posted on the Internet implicating him in corruption as "montage", a manipulation. But he must reckon with further such postings in the run-up to the presidential race.
His government has blocked access to both the social networking site Twitter and YouTube in moves condemned by Western governments and rights groups.
He could also choose to run for a fourth term as prime minister in parliamentary elections next year.
Sunday's results will come as a bitter blow to the CHP.
"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."
Erdogan formed AK in 2001, drawing nationalists and centre-right economic reformers as well as religious conservatives who form his base. Since his 2011 poll victory he has moved more towards these core religious supporters he sees as having been "looked down upon" over generations by an urban secular elite.
Whatever the scale of Erdogan's victory, he will awake on Monday to a huge task in restoring control over the security apparatus of the country. Even in purging members of the police force he considers unreliable, he cannot be sure that the replacements he brings in are loyal. (Additional reporting by Seda Sezer, Alexandra Hudson, Daren Butler and Ayla Jean Yackley, Editing by Nick Tattersall)