Turkey's Premier Recep Tayyip Erdogan claimed victory for his Islamic-rooted party in Sunday's key local elections and warned his foes they will "pay the price" for plotting his downfall.
"Those who attacked Turkey got disappointed," Erdogan told a jubilant crowd of thousands, speaking from the balcony of his party's Ankara headquarters after months of turmoil marked by street clashes, sleaze claims and an Internet clampdown.
"You have supported your prime minister. I thank you infinitely," Erdogan told the crowd, his voice still hoarse from marathon campaigning for the municipal polls that were seen as a referendum on his 11-year rule.
Erdogan, 60, has been eyeing a run for the presidency in August -- the first time voters will directly elect the head of state -- and his midnight victory speech sounded at times like the start of his next campaign.
"You have protected the independence struggle of the new Turkey," the man often called "the sultan" told his flag-waving followers, interrupted by chants of "Turkey is proud of you", "Stand tall, don't bow" and "God is great".
With 95 percent of all ballots counted, Erdogan's Justice and Development Party (AKP) emerged well ahead of its rivals on 45 percent of the vote, with the opposition Republican People's Party (CHP) on 28.5 percent.
The AKP looked headed for strong victories nationwide, including in the biggest city Istanbul and the capital Ankara, over the secular opposition.
But in Ankara the main opposition, the centre-left CHP, said the vote was close.
A definitive result in the capital awaited vote recounts, which were under way in some districts.
Erdogan again threatened to pursue his declared nemesis Fethullah Gulen, a reclusive Muslim cleric whom he accuses of running a parallel "deep state" undermining his government from faraway rural Pennsylvania.
Erdogan has accused loyalists of the 73-year-old imam in the police and justice systems of damaging leaks on Twitter and of bugging secret security talks on Syria before then releasing the explosive audio recording on YouTube.
"We will enter their caves and ... they will pay the price," he warned. "How can you threaten our national security on Syria? Syria is currently in a state of war against us."
"There won't be a state within a state," he stressed.
Months of political turmoil -- fought out in fierce street clashes and the Internet leaks -- have left Turkey deeply polarised between Erdogan's Muslim conservative supporters and a secular political camp.
Erdogan's inner circle has been hit by the damaging online leaks since December, with wide-ranging bribery and sleaze claims against the prime minister and his political and business allies going viral in the youthful country.
The spiralling crisis has sent down the Turkish lira and stock market in recent months and rattled investors' faith in a Muslim democracy that has often been described as a model for post-Arab Spring countries.
The premier's heavy-handed response to being challenged on the streets and online has included a police crackdown on protesters in Istanbul that left eight dead and thousands wounded and arrested, and blocks on Twitter and YouTube.
The crackdown has alienated NATO allies and detracted from Erdogan's much lauded record of driving an economic boom and transforming the country into an emerging global player.
"Our democracy must be strengthened and cleansed," said Kemal Kilicdaroglu, leader of the opposition CHP, as he cast his vote Sunday.
Despite a round of reprimands from foreign powers and human rights groups, the "results show us that Erdogan has survived these scandals with very little damage," Mehmet Akif Okur of Ankara's Gazi University told AFP.
"Voters believe that if Erdogan falls, they will fall with him. No matter how serious the corruption allegations are, the voters supported Erdogan to keep the status they have acquired under his rule," he added.
"Seeing that his 'authoritarian' measures have not hurt him at the ballot box, Erdogan will be more ambitious in pushing through tougher policies. I would expect lawsuits against the Gulen movement and arrests in the coming days."
Away from the national issues, local rivalries that regularly flare up at elections in Turkey left at least nine people dead Sunday.