ANKARA — 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 jubilant 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, interupted by chants of “Turkey is proud of you”, “Stand tall, don’t bow” and “God is great”.

As the vote count continued, Erdogan’s Justice and Development Party (AKP) looked headed for strong victories nationwide, including in the biggest city Istanbul and the capital Ankara, over the secular opposition.

Erdogan again threatened to go after his declared nemesis, a reclusive Muslim cleric whom he accuses of running a parallel “deep state” undermining his government from faraway rural Pennsylvania.

He has accused loyalists of 73-year-old imam Fethullah Gulen in the police and justice systems of dropping the leaks on Twitter and of bugging secret security talks on Syria, then releasing the 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.”

‘Survived scandals with little damage’

Months of political turmoil — fought out in fierce street clashes and the explosive Internet leaks — have left Turkey deeply polarised between Erdogan’s Muslim conservative supporters and a secular political camp.

Since December, Erdogan’s inner circle has been hit by the damaging online leaks, 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 the 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 spanning Europe and Asia into an emerging global player.

“Our democracy must be strengthened and cleansed,” said Kemal Kilicdaroglu, head of the main opposition Republican People’s Party (CHP), as he cast his vote Sunday.

Despite a round of reprimands from foreign capitals 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.

“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.”