By Tuvan Gumrukcu and Ece Toksabay
ANKARA (Reuters) - Turkey's Tayyip Erdogan suffered stunning setbacks in local elections as his ruling AK Party lost control of the capital Ankara for the first time since the party's founding in 2001, and was on course to lose the biggest prize of all, Istanbul.
Erdogan, who has dominated Turkish politics since coming to power 16 years ago and ruled his country with an ever tighter grip, campaigned relentlessly for two months ahead of Sunday's vote, which he described as a "matter of survival" for Turkey.
But the president's daily rallies and overwhelmingly supportive media coverage failed to win over voters in the two main cities, as Turkey's tip towards economic recession weighed heavily on voters.
Turkish broadcasters said opposition Republican People's Party (CHP) candidate Mansur Yavas had won a clear victory in Ankara. In Istanbul, the CHP was nearly 28,000 votes ahead as the last votes were being counted.
"The people have voted in favour of democracy, they have chosen democracy," opposition leader Kemal Kilicdaroglu said, declaring that his secularist CHP had taken Ankara and Istanbul from the AK Party (AKP) and held its Aegean coastal stronghold of Izmir, Turkey's third largest city.
The turnout was a very high 84.52 percent nationwide, according to state-owned Anadolu news agency.
Defeat for Erdogan's Islamist-rooted party in Ankara was a significant blow for the president. Losing Istanbul, a city three times the size of the capital, where he launched his political career and served as mayor in the 1990s, would be an even greater shock.
High Election Board Chairman Sadi Guven told reporters the CHP Istanbul candidate Ekrem Imamoglu had 4,159,650 votes, while the AKP candidate, former Prime Minister Binali Yildirim, had 4,131,761 votes.
There was a three-day period for election results to be challenged, Guven said, adding that results of 84 out of 31,186 ballot boxes in Istanbul had not yet been scanned into the system due to challenges.
The Turkish lira, which swung wildly https://tmsnrt.rs/2CEaO11 in the week ahead of the elections echoing last year's currency crisis, weakened as much as 2.5 percent against the dollar from Friday's close. [L8N21J0AR]
Anadolu said the AKP would appeal in some Ankara districts.
In a speech to supporters on Sunday night, Erdogan appeared to accept AKP defeat in Istanbul, although he said that most neighbourhoods in the city were held by his party.
"Even if our people gave away the mayorship, they gave the districts to the AK Party," he said.
The party would appeal results wherever needed, he added.
"TURNING A PAGE"
Erdogan pledged that Turkey would now focus on its troubled economy in the run-up to national elections in 2023. "We have a long period ahead where we will carry out economic reforms without compromising on the rules of the free-market economy," he told reporters.
Turkey's most prominent leader since the founder of the Turkish republic Mustafa Kemal Ataturk, Erdogan has built support based on strong economic growth and backing from a core constituency of pious, conservative Muslim Turks.
A consummate campaigner, he has been the country's most popular - although divisive - modern politician, tightening his grip in elections last year that ushered in a powerful executive presidency, approved in a bitter 2017 referendum which alarmed Western allies who fear growing authoritarianism in Turkey.
But a currency crisis after last year's election dragged the lira down by 30 percent and tipped the economy into recession in the fourth quarter. With inflation close to 20 percent and unemployment rising, some voters were ready to punish the president.
"Today's elections are as historic as that of 1994," prominent journalist Rusen Cakir tweeted, referring to the year Erdogan was elected mayor of Istanbul. "It is a declaration that a page that was opened 25 years ago is being turned."
As authorities again scrambled to shore up the lira over the past week, Erdogan cast the country's economic woes as resulting from attacks by the West, saying Turkey would overcome its troubles and adding he was "the boss" of the economy.
Wolfango Piccoli, co-president of Teneo political risk advisers, noted the AKP had lost seven of the country's 12 main cities, even without taking Istanbul into account.
"It's a bad night for the AK Party," he said. "They have done very poorly in all the economic powerhouses of country. For a party which portrays itself as pro-business, it's a huge issue."
Moody's rating agency said the central bank's use of reserves last week to prop up the lira raised new questions over its independence, while uncertainty over Turkey's policy response to recession raises the risk of further capital flight.
In mainly Kurdish southeast Turkey, residents celebrated as the pro-Kurdish opposition Peoples Democratic Party (HDP) won back municipalities that authorities had taken over two years ago, accusing the HDP of terrorist links. The HDP denies links to the outlawed militant Kurdistan Workers' Party.
"They robbed us of our will and we overturned this," Diyarbakir resident Abdullah Elmas said.
(Additional reporting by Tulay Karadeniz, Orhan Coskun and Nevzat Devranoglu in Ankara, Daren Butler and Ezgi Erkoyun in Istanbul; Writing by Dominic Evans; Editing by Jonathan Spicer, Clarence Fernandez and Peter Graff)