ANKARA (Reuters) - Turkey’s Tayyip Erdogan suffered a severe setback on Sunday as his ruling AK Party lost control of the capital Ankara for the first time in a local election and he appeared to concede defeat in the country’s largest city, 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 narrowly failed to win over the country’s capital or secure a clear result in Istanbul, as Turkey’s economic downturn weighed heavily on voters.

“The people have voted in favor of democracy, they have chosen democracy,” opposition leader Kemal Kilicdaroglu said, declaring that candidates for his secularist Republican People’s Party (CHP) had taken Ankara and Istanbul from the AK Party and held their Aegean coastal stronghold of Izmir, Turkey’s third largest city,

Defeat for Erdogan’s Islamist-rooted party in Ankara was a significant blow for the president. Losing Istanbul, where he launched his political career and served as mayor in the 1990s, would be an even greater symbolic shock and a broader sign of dwindling support.

With 99.8 percent of ballot boxes opened in Ankara, CHP mayoral candidate Mansur Yavas had secured 50.9 percent, giving him a 3.8-percentage-point lead over his AKP rival. State-owned Anadolu Agency said the AKP would appeal in some districts of the capital.

In Istanbul, Turkish TV channels showed that AKP candidate and former Prime Minister Binali Yildirim had the narrowest of leads over his rival Ekrem Imamoglu - 4,000 votes out of 8 million counted - with 98.8 percent of ballot boxes opened.

Yildirim declared victory, but his claim was challenged by Imamoglu who said CHP data showed he had an unassailable lead.

In a speech to supporters in Ankara, Erdogan appeared to accept AKP defeat in Istanbul, although he maintained that most neighborhoods 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’s support has been based on strong economic growth and backing from a core constituency of pious, conservative Muslim Turks.

Slideshow ( 26 images )

A consummate campaigner, he has been the country’s most popular - although divisive - modern politician, tightening his hold on power in elections last year that ushered in an executive presidency, approved in a bitterly fought 2017 referendum.

But a currency crisis after last year’s election dragged the lira down by 30 percent and tipped the economy toward recession. With inflation close to 20 percent and unemployment rising, some voters appeared 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.”

Slideshow ( 26 images )

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, said the AK Party 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.”

The lira traded at 5.61 to the dollar after initial results came in, compared with 5.55 at Friday’s close and 5.65 in U.S. trading hours late on Friday.

The election was marred by violence, with two people killed in Malatya province and three others in the southern province of Gaziantep, Turkey’s electoral board said.

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.