In a major setback for President Erdogan, early results showed his AK Party losing Ankara and Izmir to the opposition in local elections.

While Istanbul remains disputed, Erdogan said his party may have lost Turkey's largest city too.

Nationwide, AKP remains the largest party with 45 per cent of the vote, according to partial results released by Anadolu state news agency with 91 per cent of ballot boxes opened.

After 92 per cent of ballot boxes were opened, Republican People's Party (CHP) candidate Mansur Yavas had 50.62 per cent in Ankara, giving him a 3.4 percentage points lead over his rival from the ruling AK Party, according to partial results released by Anadolu state news agency.

In Istanbul, AKP candidate and former prime minister Binali Yildirim had a narrowing lead of 0.14 percentage point over the main opposition party after 98 per cent of the ballots were opened. Both the AKP and opposition candidates were claiming victory in Istanbul late on Sunday evening.

Reacting to the release of early results, President Erdogan said his party was leading by far across Turkey and that losing some cities was 'the necessity of democracies'.

Erdogan also said the next elections would be held in June 2023 and that Turkey would carefully implement a "strong economic programme" without compromising on free market rules.

READ MORE: WhyTurkey's local elections have become a key national battle?

A highly divisive vote

The vote is one of the most divisive elections In Turkey's modern history and was marred by violence, with two members of the Islamic Felicity Party shot dead, a party spokesman said.

Erdogan has described the vote as a matter of survival for the country.

Turkey's economy has been declining following a currency crisis last year when the lira lost more than 30% of its value. It has forced prices up, making the cost of living more difficult for many.

Erdogan has blamed the country's economic woes on attacks by the West.

“The aim behind the increasing attacks toward our country ahead of the elections is to block the road of the big, strong Turkey,” Erdogan said on Saturday.

The President has also pointed the finger at interest rates and said if the central bank lowers them, price pressures will come down too and that Turkey would overcome its troubles following Sunday’s vote.

Erdogan has dominated Turkish politics for more than 16 years, largely down to strong economic growth, but a defeat in Ankara or Istanbul would bring an end to the AK Paty's two-decade rule.

The main opposition Republican People’s Party (CHP) and Iyi (Good) Party formed an electoral alliance to rival the AKP and its nationalist MHP partners.

The pro-Kurdish opposition Peoples Democratic Party (HDP), which Erdogan has accused of links to Kurdish militants, which it denies, has not made an official alliance and is not fielding candidates for mayor in Istanbul or Ankara.

Erdogan has waned at rallies that if the opposition candidate wins in Ankara, residents would “pay a price” and accused his rivals of terrorist supporters aiming to ruin Turkey.

His opponents, however, have denied the accusations and said Erdogan is responsible for leading Turkey to its current state.

“What matter of survival? We’re electing mayors. What does this have anything to do with the country’s survival?” Kemal Kilicdaroglu, head of the CHP, told a rally in Eskisehir.

With reference to Erdogan, Kilicdaroglu said: “If there is a survival issue in Turkey, it’s because of you.”

READ MORE: Hagia Sophia: Controversy as Erdogan says museum and former cathedral will become a mosque