Turkey’s capital has slipped from President Recep Tayyip Erdoğan’s grasp.

Erdoğan’s Justice and Development Party (AKP) lost control of Ankara in Sunday’s municipal elections, while the mayoral race in Istanbul descended into chaos as both the government and opposition claimed victory in Turkey’s largest city.

Sunday’s vote, which was seen as a litmus test of the president’s popularity, marked a significant setback for Erdoğan, and he acknowledged that his party had lost support.

“We, as the AKP, have lost some of the municipalities,” Erdoğan said in a speechlate Sunday. “We will accept that we have won the hearts of our people in the places where we won, and we were not successful enough in the places where we lost, and we will decide on our action plan accordingly.”

Preliminary results show the secularist Republican People’s Party (CHP) candidate Mansur Yavaş firmly on track to win in Ankara, with Turkish broadcasters already declaring a clear opposition win in the capital.

Amid Turkey’s first recession in a decade, local issues took a backseat during the campaign as the municipal elections turned into a litmus test of Erdoğan’s popularity.

But the race for mayor of Istanbul was so tight, the result remains uncertain.

In a nail-biting vote count broadcast live on television, the government and opposition candidates for Istanbul mayor were less than 0.1 percentage points apart when the state news agency Anadolu stopped updating the count late Sunday.

AKP candidate Binali Yıldırım, the former prime minister, then declared victory — even though about 2 percent of ballots remained uncounted and the gap between him and opposition candidate Ekrem İmamoğlu amounted to a few thousand votes in a city of 10 million voters.

İmamoğlu, a CHP candidate backed by its opposition allies, cried foul, claiming his party’s count as well as the official numbers from Turkey’s electoral council showed him winning Istanbul. The council’s website was down as of Monday morning.

Opposition candidate Ekrem İmamoğlu is locked a tight race for mayor of Istanbul | Yasin Akgul/AFP via Getty Images

Opposition candidate Ekrem İmamoğlu is locked a tight race for mayor of Istanbul | Yasin Akgul/AFP via Getty Images

In another speech Sunday, Erdoğan appeared to hint the government had lost Istanbul, saying: “Even if our people gave away the mayorship, they gave the districts to the AKP.”

But his spokesman later said on Twitter the president’s words did not contradict Yıldırım’s declaration of victory.

Despite losing several provinces, the AKP and its allies, the Nationalist Movement Party (MHP), won a majority of the nationwide vote share with 52 percent at 99 percent of ballots opened.

Tensions ran high on election day amid a series of close races, with violence erupting at several polling stations. Four people were killed in election-related violence, the interior ministry said, while the broadcaster NTV reported six deaths. Local media also reported dozens of people injured in fights.

Amid Turkey’s first recession in a decade, local issues took a backseat during the campaign as the municipal elections turned into a litmus test of Erdoğan’s popularity.

For years, Erdoğan successfully cast himself as the guarantor of prosperity and growth, but as living costs soared — with inflation at around 20 percent and food-price increases hitting above 30 percent — discontent grew widespread, even among the AKP’s conservative voter base.

The president himself described the elections as an existential test for his country, calling the vote “a matter of survival.”

Supporters of the main opposition party cheer as the results come in at a venue in Istanbul | Yasin Akgul/AFP via Getty Images

Supporters of the main opposition party cheer as the results come in at a venue in Istanbul | Yasin Akgul/AFP via Getty Images

But neither Erdoğan’s rhetoric nor his government’s subsidized vegetable schemewere enough to convince voters in many of Turkey’s cities — even with the playing field skewed firmly in favor of the president, whose allies control the vast majority of mainstream media outlets.

Ankara was not alone in swinging to the opposition: The AKP also lost Adana, Antalya and Mersin — all among Turkey’s 10 largest cities — and a number of Anatolian provinces, according to preliminary results.

The CHP, on the other hand, defended its strongholds on the Aegean coast, including Turkey’s third-largest city of Izmir.

Voters in the eastern Anatolian province of Tunceli, meanwhile, elected a mayor from the Communist Party, handing the Communists control over a provincial capital for the first time in Turkey’s history.

The opposition’s municipal victories may be unlikely to loosen Erdoğan’s tight hold over Turkey; since winning reelection last year, he governs as executive president with sweeping powers.

But they still are a severe blow to Erdoğan — signaling that after 17 years in power, his popularity may be declining. Sunday’s results will also breathe new life into the long-stagnant opposition.

Erdoğan has hinted he will not respect some election results.

Moreover, in losing metropolitan municipalities, the AKP has lost control over a patronage network that allows city governments to hand contracts to sympathetic businesses.

Defeat in Ankara’s mayoral race marks a significant loss for the ruling party, as Erdoğan’s religious-conservative movement has held power in the capital for a quarter century.

Defeat in Istanbul would hit Erdoğan particularly hard, however. The president himself began his meteoric rise as mayor of Turkey’s largest city in the 1990s and has said that “whoever wins Istanbul, wins Turkey.”

After six votes in five years — five elections and one referendum — Sunday marked Turkey’s last scheduled ballot until 2023.

Yet some of the opposition’s municipal victories could be short-lived. Erdoğan has hinted he will not respect some election results, vowing to remove some 300 city councillor candidates with alleged links to terror groups.

As part of a sweeping crackdown following a coup attempt in 2016, Erdoğan’s government has removed dozens of mayors and hundreds of local officials with alleged links to Kurdish militants, replacing them with government-appointed trustees.