Turkish President Recep Tayyip Erdogan's ruling party suffered a major upset after local election results showed it lost the capital Ankara and Istanbul after a decade and half in power.

Losing the country's two major cities would be a stunning defeat for Mr Erdogan, a former Istanbul mayor himself, who has dominated Turkish politics since coming to power 16 years ago

Mr Erdogan campaigned hard, portraying the vote for mayors and district councils as a fight for the nation's survival.

However, the election became a test for his Justice and Development Party (AKP) after Turkey slipped into a recession for the first time in a decade. 

The opposition CHP party candidate for Istanbul mayor, Ekrem Imamoglu, was leading by nearly 28,000 votes with most ballot boxes counted, Supreme Election Board (YSK) chairman Sadi Guven said.

Mr Imamoglu won almost 4.16 million votes while the AKP candidate, former premier Binali Yildirim, won 4.13 million.

Both claimed victory in the early hours following a tight race for the country's largest city after results showing them in a dead heat.

"We want to start working as soon as possible to serve people," Mr Imamoglu told reporters on Monday.

"We want to cooperate with all institutions of Turkey to quickly meet the needs of Istanbul."

In Ankara, opposition mayoral candidate Mansur Yavas had 50.89% of votes ahead of the AKP's Mehmet Ozhaseki on 47.06%, Anadolu state agency reported, with 99% of ballot boxes counted.

"Ankara has won. The loser in Ankara is Ozhaseki, dirty politics has lost," Mr Yavas told supporters who were waving Turkish flags and setting off fireworks at a celebratory rally.

But in a sign of possible turmoil ahead, AKP officials said they would challenge the alleged invalidation of tens of thousands of votes in both cities.

AKP secretary general Fatoih Sahin said the party will appeal in Ankara, saying the gap between the candidates "will narrow down and I believe it will eventually turn into a positive result for us".

Speaking to supporters in Ankara after Sunday's polls closed, Mr Erdogan had said the election was a victory for the AKP, which along with its coalition partner, the rightwing Nationalist Movement Party, won more than 50% of votes overall.

The loss in Istanbul, analysts said, would be especially sensitive for Mr Erdogan, who grew up in the city's working-class Kasimpasa neighbourhood.

For his supporters, Mr Erdogan remains the strong leader they believe Turkey needs and they tout the country's economic development over the years he and the AKP have been in power.

Rallying his base among more religiously conservative Turks, Mr Erdogan had presented his opponents as enemies of the state, tarnishing them as tied to Kurdish militants of the Kurdistan Workers' Party (PKK) who have fought a decades-long insurgency.

Rights activists and Turkey's Western allies say that democracy has been eroded under Mr Erdogan's leadership, particularly after a failed 2016 coup that led to tens of thousands of people being arrested.