Both the AKP and the main opposition Republican People's Party (CHP) claimed victory in Istanbul, Turkey's largest city and economic hub.
As a result, the conservative Justice and Development Party (AKP), chaired by the Turkish leader, lost the municipal elections in the Turkish capital.
Think your friends would be interested? CHP-supported had 4,111,219 votes with 98.8 percent of ballots counted, ahead of the AK-backed candidate with 4,106,776.
Meanwhile, the race in Turkey's largest city, Istanbul, remained too close to call.
Yavas had been slightly ahead in some recent opinion polls before the election.
The overall share of the votes for Erdogan's alliance, however, didn't change much since last year's presidential vote as he maintained his hold on the interior of the country.
The HDP is alleging electoral fraud in one such city, Sirnak, which it carried in the last election with 59.6 percent of the vote, but lost this time to the AKP, which won 61.9 percent of the vote.
"Every gain and every loss is the will of our people and also a requirement of democracy that should be acknowledged", Erdoğan said in Istanbul before flying to Ankara.
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.
Imamoglu said in a later press conference that he won the race by more than 29,000 votes.
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.
Central Bank "backdoor tightening" over the last week to prevent a repeat of last year's lira crisis, when the currency fell 30 percent, "did not really increase confidence in their policy making", Leuchtmann said.
Earlier, in a victory speech, Erdogan said 56 percent of municipalities in Turkey will be run by his AKP.
He argued, however, that a pause in elections until 2023 would benefit Erdogan. The opposing camp led by the Republican People's Party, or CHP, had 37.6 percent, Anadolu reported.
During his long tenure - first as prime minister, then as president - Erdogan has drawn strong support from ordinary Turks grateful for the stability and growth he brought, from liberals who backed his efforts to curb Turkey's military, and from Kurdish groups who entered into peace talks with Ankara. 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".
In a Monday statement, Iranian Foreign Ministry Spokesman Bahram Ghasemi congratulated neighboring Turkey over the successful holding of local and municipality elections, noting that democracy in the regional countries is the guarantee for the region's independence, security and stability.
Binali Yildirim, the Justice and Development Party's candidate, on Monday accepted that his opponent Ekrem Imamoglu was leading in Sunday's election by about 25,000 votes in Istanbul.