The opposition says shrinking support for Erdogan would mark the beginning of the end of his 16-year rule.
Sunday's local elections were widely seen as a gauge of support for Erdogan as the nation of 81 million faces a daunting economic recession. The Turkish leader was sworn in with nearly untrammelled powers after last year's general elections that followed a 2017 constitutional amendment to change Turkey's political framework into an executive presidency from a parliamentary system.
If confirmed, the swings in Ankara and Istanbul could be excruciating for the politician who campaigned hard to retain hold of them.
If the opposition won in Istanbul, Turkey's largest city and commercial hub, that would be a watershed moment.
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. But he did not refer directly to the loss of Ankara.
"Turkey's regime now signals that it won't give up Istanbul, regardless of vote distribution", Timur Kuran, professor of economics and political science at Duke University, said on Twitter. "The basic problem is that Erdogan is not able to get votes from middle-income earners, who believe that the economy, education and urban administration are not run well".
But in crucial mayoral races, the CHP declared victory in the capital, Ankara, and in the major city of Izmir.
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.
Ekrem Imamoglu, the candidate from the main opposition Republican People's Party (CHP), had won 48.79 percent of the votes, as against 48.51 percent for Yildirim, according to the latest data unveiled by the state-run Anadolu Agency.
"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. Erdogan's Justice and Development Party has been in control of the city for over 15 years. Erdogan's own ascent to power began there in 1994.
"In Istanbul, the majority of the districts are either ours or head-to-head".
The government had led a hostile campaign against Yavas and his party, accusing him of forgery and tax evasion.
Preliminary results showed the opposition flipping the capital Ankara and surrounding areas from Erdogan's alliance, and taking control of key Mediterranean coastal cities. Both parties had earlier claimed victory.
Erdogan plans to respond to the elections with stepped-up austerity and war.
But the recent economic troubles took a toll on party support, two AK Party sources said, after the lira slumped against the dollar a year ago, inflation jumped to 20 percent, unemployment climbed and the economy tipped into recession.
The official added that holding peaceful elections is another sign of democracy in Turkey.
In predominantly Kurdish provinces, a pro-Kurdish party won seven top mayoral seats but lost some strongholds, including southeastern Sirnak province, to the ruling party.
The Turkish leader did not directly mention the full election results there or in Istanbul.
Political parties have three days to submit objections and official results are expected in the coming days.