Ekrem Imamoglu of the Republican People’s Party called his election win Sunday a victory for “all of Istanbul.” (Lefteris Pitarakis/AP)

Opposition party candidate Ekrem Imamoglu won the revote for Istanbul mayor by a wide margin Sunday, handing Turkish President Recep Tayyip Erdogan a stunning political defeat, according to preliminary results.

Ruling party candidate Binali Yildirim, a former prime minister, conceded the race in a televised speech shortly after the results showed Imamoglu leading, with nearly 54 percent of the votes. Erdogan, who fought tooth and nail to retain the mayor’s seat for his party, offered his congratulations on Twitter a short time later.

“The national will has manifested itself once again today. I congratulate Ekrem Imamoglu, who has won the election according to unofficial results,” he wrote, in a rare instance when the president referred to the opposition candidate by name.    

Faith in Turkey’s elections was shaken after the first mayoral vote in March. Erdogan’s Justice and Development Party, or AKP, challenged results that showed Imamoglu, of the Republican People’s Party, or CHP, had won by a slim margin.

The state election board annulled the vote and ordered a rare repeat of the contest, sparking criticism that the board had succumbed to political pressure.

The resulting political turmoil unnerved Turkey’s allies, including the Trump administration. U.S. officials complained privately that negotiations with Ankara on sensitive issues, including Syria, were being disrupted by the caustic election rhetoric.

Imamoglu’s win Sunday gave Turkey’s main opposition party nominal control of Istanbul, Turkey’s largest city and its commercial hub, after decades of dominance by the AKP. Erdogan’s legendary political gifts, including his ability to sway elections by force of personality and to mobilize his sizable base of supporters, failed him, at least for the moment.

The Turkish president at one point campaigned daily for Yildirim, staking his own storied political brand on the outcome of the race. The AKP spared no effort in recent weeks to turn out the party faithful — voters who had propelled Erdogan and his allies to victory time and again.

Preliminary results showed those efforts faltered badly, with the AKP losing votes in several districts that it considered strongholds. A last-ditch effort by the AKP to peel away Kurdish voters also failed.

Sinem Adar, the Einstein Senior Fellow at Humboldt University in Berlin, said Erdogan’s advisers appeared to have misled him by failing to make him aware of the scale of disaffection among his own base. Erdogan faces the possibility of defections by prominent figures in the party because of the size of the election loss, she said. 

“Erdogan is significantly weakened,” she said.

The president’s political future remains secure, for now: He was reelected last June to a five-year term. But with his aura of invincibility pierced by the result Sunday, the hand-wringing and self-reflection in his party had begun.

In a message on Twitter, AKP lawmaker Mustafa Yeneroglu wrote that the party “lost Istanbul because we lost our moral superiority. We can hope again with sincere self-criticism.”

He added that the party needed to “focus on the dreams of young people, rationality, rule of law.”

The loss was especially bitter for Erdogan because it occurred at home. He was born and raised in Istanbul and soared to political prominence here 25 years ago as mayor. The city served as the financial wellspring for the ruling party’s patronage networks and a showcase for Erdogan’s ambition, including his fondness for megaprojects. The massive, sprawling new Istanbul Airport, which opened earlier this year, was only the latest.  

Ahead of Sunday’s revote, thousands of volunteers were mobilized to monitor balloting at more than 30,000 polling places — a determined effort to prevent the election board any grounds for another cancellation.

The two candidates appeared in a rare televised debate watched by millions of Turks. They quibbled over the March election, mostly, but some differences in their policies also emerged.

Yildirim promised more of the high-profile development projects his party has championed over the years, and which he said would create tens of thousands of jobs.

Imamoglu, fiddling with props, promised financial aid for put-upon working-class voters. They both promised that Istanbul, a city suffocating from stampeding development, would be provided with more green space.

After nightfall Sunday, Imamoglu’s supporters celebrated on the streets of CHP districts, chanting his campaign slogan: “Everything will be all right.”

Turkey’s faltering economy and the soaring cost of basic goods were central concerns in both elections. But this time, some people also said they were angered by the cancellation of March’s vote, viewing it as a cynical manipu­la­tion of Turkey’s democracy.

At a polling station in the conservative AKP stronghold of Fatih on Sunday, Kader Esen, 32, said she and most of her extended family voted for the ruling party candidate in March.

But on Sunday, she said, the family — roughly 30 people — voted for Imamoglu instead.

“I changed my vote because I thought an injustice was done,” she said.

Imamoglu, a mild-mannered former mayor of Istanbul’s Beylikduzu district, won praise for his conciliatory, nonpartisan rhetoric and pledges of financial assistance.

“No one party, no group, no sect won this election,” he said Sunday night. “All of Istanbul has won.”

He had pledged to redirect tens of millions of dollars that he said flowed from the city’s coffers every year to Erdogan and the AKP’s favored organizations and religious foundations.

“Serving a man, an individual, a group, congregations, foundations, associations — that’s all over,” Imamoglu said in April. Instead, the money would be sent to establish day-care centers and other institutions that serve more of Istanbul’s citizens.

But his ability to effectively govern Istanbul remained in doubt, Adar said. The AKP might lose access to its patronage networks, but the party still controls Istanbul’s city council, which can obstruct Imamoglu’s initiatives.  

Soner Cagaptay, author of a book on Erdogan, said local governments struggle because Turkey is highly centralized and the state largely controls the national purse strings. But he warned that any attempt by Erdogan to hamstring Imamoglu — by withholding funds or obstructing his initiatives, for example — could backfire.  

Such efforts would only burnish Imamoglu’s reputation as an underdog. And Turks “love to support the underdog,” he said, noting that Erdogan’s own political fortunes rose because he was seen, especially by conservatives in Turkey, as an outsider and a champion of the marginalized.  

Now, Cagaptay said, “Erdogan has become the establishment.”