Erdogan's ruling AKP suffers setback in Turkey's local election
Losing Turkey's two major cities would be a clear setback for Erdogan and his Justice and Development Party (AKP) who won every vote in a decade and a half in power thanks in part to economic growth.
Erdogan portrayed the vote for mayors and district councils as a fight for Turkey's survival, but the election was a test for the AKP as an economic slowdown took hold after a collapse of the lira currency.
With 99 percent of the ballot boxes counted, the joint opposition candidate for Ankara mayor, Mansur Yavas was winning with 50.89 percent of votes and the AKP on 47.06 percent, Anadolu state agency reported citing preliminary results.
In Istanbul, Turkey's largest city, the race for mayor was deadlocked with the AKP candidate claiming victory with 48.70 percent of votes, but his opponent on 48.65 percent also saying he had won, after almost all ballot boxes were counted there.
The last results published by Anadolu gave the AKP a lead of just 4,000 votes and the ruling party said it planned to challenge tens of thousands of ballots it considered invalid in both of the major cities.
Speaking to thousands of supporters in Ankara, Erdogan portrayed the election as a victory for AKP, which along with coalition partner, the rightwing Nationalist Movement Party (MHP), won more than 50 percent of the votes nationwide. But he did not refer directly to the loss of Ankara.
"If there are any shortcomings, it is our duty to correct them," Erdogan told supporters. "Starting tomorrow morning, we will begin our work to identify our shortcomings and make up for them."
He suggested if his party lost in Istanbul, they would still control district councils even if the opposition held the mayor's office.
Sunday's poll was the first municipal ballot since Turks approved constitutional reforms in 2017 to create an executive presidency that gave Erdogan wider powers after 16 years in office.
But Erdogan, whose ability to win continuously at the polls is unparalleled in Turkish history thanks to support among more pious, conservative Turks, was more vulnerable with the economy in recession, unemployment higher and inflation in double digits.
Ankara fireworks
For his supporters, 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.
But rights activists and even Turkey's Western allies say that under Erdogan's leadership, democracy has been eroded, particularly after a failed 2016 coup that led to tens of thousands of people being arrested.
Much of the AKP's success has been down to Erdogan's perceived economic prowess, but days before the vote, the Turkish lira was sliding again, provoking memories of the 2018 currency crisis that badly hurt Turkish households.
In Ankara, Yavas - the candidate for both the opposition Republican People's Party or CHP and the nationalist Good Party - claimed victory in a large rally full of supporters waving red Turkish flags and setting off fireworks.
"No one has lost. Ankara has won. All of Ankara has won, hand in hand," he told supporters.
Yavas had been slightly ahead in some recent opinion polls before the election.
"Erdogan is known with his success in the local elections and his model of government is highly based on his local experience," Emre Erdogan, a professor at Istanbul Bilgi University and no relation to the president.
"These losses will harm his reputation as a good local politician."
Istanbul dead heat
In Istanbul, a city where Erdogan had sometimes described victory as like winning Turkey itself, the race had been very tight. Erdogan fielded one of his loyalists, former prime minister Binali Yildirim, in a push to win the city.
Erdogan, who began his own political career as Istanbul mayor, personally campaigned hard across Turkey, often with several rallies a day, even though he was not on the ballot. He was often rallying in Istanbul's districts.
"We have won the election in Istanbul. We thank Istanbul's residents for the mandate they have given us," Yildirim told supporters as final tallies were arriving.
But his opponent Ekrem Imamoglu dismissed Yildirim's claim as an attempt to manipulate opinion.
"I would like to announce to Istanbul's residents and all of Turkey that our numbers show that it is clear we won Istanbul," Imamoglu said in a speech in the early hours of Monday.
Looking to galvanise his base among conservative Turks, the president cast the election as a matter of survival, attacking opposition candidates by branding them as linked to PKK Kurdish militants.
Observers say that with most media pro-government, opposition parties campaigned at a disadvantage because Erdogan's daily rallies dominated TV coverage.
The opposition pro-Kurdish Peoples' Democratic Party (HDP) has refused to field candidates in several cities, saying the elections are unfair. Some of its leaders have been jailed on terror charges, accusations they reject.