ANKARA/ISTANBUL (Reuters) - Tayyip Erdogan’s ruling AK Party was on track on Monday to lose control of Turkey’s two biggest cities, Istanbul and Ankara, in a stunning local election setback that could complicate the president’s plans to combat recession.

While official vote tallies and Turkish broadcasters put the main opposition Republican People’s Party (CHP) candidates ahead in both cities, the Islamist-rooted AKP promised appeals that could delay the final results for days.

Erdogan, who has dominated Turkish politics since sweeping to power 16 years ago and has ruled 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 his daily rallies and overwhelmingly supportive media coverage failed to win over many urban voters, as last year’s punishing currency crisis weighed heavily on Turks.

“The people have voted in favour of democracy, they have chosen democracy,” said opposition leader Kemal Kilicdaroglu, whose secularist CHP also held its Aegean coastal stronghold of Izmir, Turkey’s third largest city.

The AKP and its Islamist predecessor have controlled Istanbul and the capital Ankara for 25 years. The results, which were still being tallied on Monday evening, would likely bring personnel changes at the highest ranks of government, according to sources inside and close to the AKP.

In Istanbul, the country’s largest city and economic hub, the CHP mayoral candidate was more than 22,000 votes ahead of his AKP opponent as the last votes were being counted, according to the country’s electoral board.

Yet the AKP also claimed victory, saying it had “plenty of” evidence of voting irregularities.

In Ankara, Turkish broadcasters said the CHP candidate had won a clear victory. But the AKP said it would appeal and expected to shift the outcome in its favour.

Slideshow ( 21 images )

Erdogan’s ruling alliance, including the nationalist MHP, captured 51.7 percent of the nationwide vote, according to state-owned Anadolu news agency. Turnout was a very high 84.5 percent.

But the loss of Istanbul - if confirmed - would be especially significant as Erdogan served as as the city’s mayor in the 1990s.

The Turkish lira, which swung wildly tmsnrt.rs/2CEaO11 in the week ahead of the elections, echoing last year's currency crisis, initially weakened on Monday as much as 2.5 percent before recovering. [L8N21J0AR]

An AKP official and a source close to the party each predicted a cabinet shuffle or other changes among those around Erdogan. [uL8N21J2OZ]

“There will certainly be changes in some places, such as Erdogan’s close circle in the party and the cabinet,” said the official, who requested anonymity. “Markets expect that there will be a change in the cabinet. This makes a change necessary.”

MARKETS ON EDGE

Erdogan, Turkey’s most prominent leader since the founder of the Turkish republic, Mustafa Kemal Ataturk, has built support based on strong economic growth and backing from a core constituency of pious, conservative Muslim Turks.

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 that alarmed Western allies who fear growing authoritarianism in Turkey.

Slideshow ( 21 images )

But a currency crisis beginning in August sliced the lira’s value by 30 percent and tipped the economy into recession. With inflation close to 20 percent and unemployment rising, some voters were ready to punish the president.

Erdogan pledged late on Sunday he would now focus on Turkey’s troubled economy ahead of national elections in 2023.

“We have a long period ahead where we will carry out economic reforms without compromising from the rules of the free market economy,” he said.

But investors are sceptical that AKP’s long-promised reforms can transform a slumping economy hooked on foreign debt, and also worry about fraying diplomatic ties with the United States that could bring more sanctions. Last week authorities used a series of stop-gap measures to cushion the selloff of Turkish assets.

Investors fear Erdogan’s electoral losses will lead him “to be more defensive, trying to shore up electoral support via populist measures, which increases risks for markets”, said Inan Demir, senior emerging market economist at Nomura, in London.

The head of the High Election Board told reporters there was a three-day period for the election results to be challenged.

The CHP’s mayoral candidate in Istanbul, Ekrem Imamoglu, was leading his AKP rival, former prime minister Binali Yildirim, by 22,479 votes, according to state agency Anadolu.

CHP candidate Mansur Yavas won the mayoral race in Ankara, preliminary results showed.

In mainly Kurdish southeast Turkey, residents celebrated as the pro-Kurdish opposition Peoples Democratic Party (HDP) won back municipalities that authorities had taken control of two years ago, accusing the HDP of terrorist links. The HDP denies links to the outlawed militant Kurdistan Workers Party (PKK).

“They robbed us of our will and we overturned this,” said Abdullah Elmas, a resident of Diyarbakir, the region’s biggest city.

A European group observing Turkey’s local elections criticised curbs on the free expression of citizens and journalists, saying on Monday it was unsure whether the Turkey electoral environment was free and fair.