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 toward 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 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.

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.