The candidate from Turkey's main opposition party appeared headed for victory in the race to become mayor of Istanbul, an influential post that launched the political career of the country's president and one he had zealously sought to keep in the hands of his ruling party.
Sadi Guven, the head of Turkey's high election board, told reporters that the opposition candidate, Ekrem Imamoglu, was leading his competitor, Binali Yildirim, by nearly 28,000 votes.
Yildirim, who served as prime minister under President Recep Tayyip Erdogan, acknowledged his opponent's lead but did not concede defeat, saying that votes were still being counted and that more than 300,000 ballots had been declared invalid - raising the possibility of a legal challenge.
The race was part of nationwide local elections held yesterday that were widely seen as a referendum on Erdogan's policies, including his faltering effort to prevent a downturn in the economy.
Preliminary results showed the ruling Justice and Development Party, or AKP, led all other parties among voters. But opposition victories in several of Turkey's largest cities, including Ankara, the capital, dealt Erdogan a significant symbolic defeat.
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.