By Tuvan Gumrukcu and Ece Toksabay

ANKARA (Reuters) - Turkey's Tayyip Erdogan suffered a severe setback on Sunday as his ruling AK Party was set to lose 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 near constant media coverage narrowly failed to win over the country's two main cities, 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) won in Ankara, Istanbul and the Aegean city of Izmir.

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.

With more than 92 percent of ballot boxes opened in Ankara, CHP mayoral candidate Mansur Yavas, had secured 50.6 percent, giving him a 3.4-percentage-point lead over his AKP rival.

In Istanbul, Turkish TV channels showed that AKP candidate and former Prime Minister Binali Yildirim had the narrowest of leads with 98.8 percent of ballot boxes opened, securing 4,111,219 votes against 4,106,776 for his rival, Ekrem Imamoglu.

Yildirim declared victory, but his claim was challenged by Imamoglu who said votes were still being counted. Broadcasters stopped updating their vote counts after Yildirim's declaration.

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.

Story continues

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.

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 towards 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."

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.

(Reporting by Tuvan Gumrukcu and Ece Toksabay; Additional reporting by Ezgi Erkoyun; Writing by Tuvan Gumrukcu and Daren Butler; Editing by Dominic Evans, Susan Fenton, Peter Cooney and Daniel Wallis)