ISTANBUL — President Recep Tayyip Erdogan confronted the prospect of a stunning political defeat on Monday, as local voting in Turkey showed his party had lost the capital, Ankara, and possibly Istanbul, its largest city and his key base of support for many years.

The results of the municipal balloting on Sunday from around the country was a telling barometer of Mr. Erdogan’s weakened standing with voters, as Turkey’s economy has fallen into a recession and he has assumed sweeping new executive powers.

Mr. Erdogan was not conceding defeat on the results in Istanbul, which were still unofficial. But the head of the High Election Council said the opposition mayoral candidate, Ekrem Imamoglu, was leading the Istanbul race by 27,806 votes, with only 24,000 remaining ballots to be counted.

“The mathematics of the issue is over,” Mr. Imamoglu told a news conference, asserting there was no way that the candidate of Mr. Erdogan’s Justice and Development Party, former Prime Minister Binali Yildirim, could catch up.