Imamoglu's victory in Istanbul, if confirmed, would be the harshest blow.
Erdogan rose to national prominence as the city's mayor from 1994 to 1998. The city has served since then as a source of wealth and prestige for his party, and a showcase - with its sprinting construction, megaprojects and multiplying mosques - for his broader ideological vision.
"We will start our work to detect our shortcomings," Erdogan said in a speech.
The election for mayors, municipal council members and other local posts represented the first nationwide referendum on Erdogan's leadership since he won a presidential poll in June. Since then, Turkey's economy has slipped into a recession for the first time in a decade, forcing the government to defend policies that have unnerved investors and sent prices soaring.
A crackdown on the government's opponents that intensified after a failed coup in 2016 has added to the domestic unease and has also troubled Turkey's relationships with Western allies, causing sharp fluctuations in the Turkish Lira, putting investors on edge.
While his own position was secure, Erdogan still raced around the country in recent weeks, attending campaign rallies at a punishing clip that he referred to as a "marathon." His bellicose rhetoric - red-meat appeals to his conservative Muslim base and menacing language leveled at his opponents, some of whom he labelled "terrorists" - underscored the stakes in the election.
"It is a matter of survival," Erdogan said of the poll, at a rally in a working-class district of Istanbul on Friday.
But even as the president framed the elections in terms of national security, many voters "did not buy it," said Murat Yetkin, a Turkish political analyst and author of a blog called the Yetkin Report. "The pollsters were telling us that 80 per cent were seeing the economy - namely the cost of living and unemployment - as the biggest problem," he said.
"This is a major loss. If the toll includes Istanbul, finally - five big cities of Turkey, including Ankara - some 40 percent of the population," would be under opposition control, potentially creating governing problems for Erdogan, he said.
The ruling party's loss of the capital Ankara, a symbol of political power, was also significant, said Soner Cagaptay, an expert on Turkey at the Washington Institute for Near East Policy. But the loss of Istanbul "would be a nuclear defeat," he said - one that diminished Erdogan's aura as Turkey's "invincible politician."
The Turkish elections at one point threatened to become an international diplomatic crisis, when Erdogan was criticized by officials in New Zealand for repeatedly airing footage at his rallies of the mosque shootings in Christchurch that killed 50 people in March.
The Trump administration was closely watching the ballot and hoping that a catalogue of disputes with Ankara - including arguments over the war in Syria and Turkey's purchase of a Russian air defense system - would quiet once Turkey's caustic campaign season had come to an end.