But a currency crisis after last year's election dragged the lira down by 30 percent and tipped the economy towards recession.
Elsewhere, Istanbul police detained seven people after a fight between candidates for muhtar (a village chief) in the Esenyurt district. The CHP retained control of Turkey's third city Izmir by a comfortable margin.
Economic prosperity provided Erdogan and his party with previous election victories.
The AKP built its ballot box success on 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.
However, analysts say Erdogan struggled to change the agenda away from the economy.
President Erdogan pledged that Turkey would now focus on its troubled economy in the run-up to national elections in 2023.
"The losses in the largest cities will lead him to be more defensive, trying to shore up electoral support via populist measures, which increases risks for markets, for fiscal policy and monetary policy", he said. On Saturday, Erdogan threatened: "We will definitely solve the Syria issue on the field, if possible, and not at the negotiating table, as out first task after elections". Let's say the prices of cucumbers or tomatoes.
This election result shows that-as in the 2011 uprisings in Egypt and Tunisia and the current mass protests demanding the bringing down of the military regime in Algeria-effective opposition will come only from the working class, in a rebellion against the entire political establishment. The AKP still holds a majority of Ankara's 25 districts.
"I am the boss of the economy right now as president of this country", he said, also blaming the West and particularly the USA for its financial turbulence.
The government seized over 100 municipalities the Kurdish party had won in 2014 and jailed some 60 mayors in an ongoing crackdown that began in late 2016. He said the party would work to understand and fix where they failed.
In Ankara, Mansur Yavas - the candidate for both the opposition Republican People's Party or CHP and the nationalist Good Party - had been slightly ahead in some recent opinion polls before the election. But the loss of Istanbul "would be a nuclear defeat", he said - one that diminished Erdogan's aura as Turkey's "invincible politician".
While courting Turkish nationalist voters Erdogan has targeted the HDP, calling the party terrorists partly linked to Kurdish insurgents the PKK.
The mayor's seat in the city of 15 million is hugely important.
Turkey's Supreme Election Council put the CHP on 4,159,650 votes, narrowly ahead of the AKP's 4,131,671. Felicity's leader, Temel Karamollaoglu, alleged a polling station volunteer and a party observer were shot by a relative of a ruling party candidate.
After voting in Istanbul, Erdogan said the incident had "upset" him and that an official investigation had been launched.
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. The old Turkish political adage "who controls Istanbul, control's Turkey", for many still holds true.