The Republican People's Party's Imamoglu won with 54.21% of the vote, beating the ruling Justice and Development Party's Binali Yildirim by more than 806,000 votes. Yildirim swiftly conceded Sunday night, congratulating his opponent.
The mayor-elect's margin of victory dwarfed the 13,729 votes that separated the two candidates in the March 31 election Imamoglu also won. After that vote, both Yildirim and Imamoglu initially claimed victory. Erdogan's ruling party, also known by the acronym AKP, filed a series of objections with Turkey's election authorities, leading to weeks of partial recounts.
The electoral board annulled the election after Imamoglu served 18 days in office and ordered the election rerun held Sunday.
Political analysts credited the surge in Imamoglu's support to his intense campaigning and a sense of injustice many voters felt after the March result was thrown out. Nearly 10.6 million voters were eligible to cast ballots Sunday, and turnout reached almost 85%.
Istanbul, along with the Turkish capital of Ankara, will both have mayors from the secular opposition party after being led for 25 years by the Islamic-leaning ruling party and its predecessor.
Imamoglu has promised to get to work for the city of more than 15 million residents, hoping to draw a line under months of political tension from the disputed mayoral election.
Istanbul is the cultural and commercial heart of Turkey, accounting for nearly a third of its GDP. The local government had a budget of $8.8 billion last year to run the city.
The new mayor said he would lift up the quarter of Istanbul's population that lives in poverty, increase green spaces and end what he has described as the governing party's "squandering" of public funds.
He alleges Erdogan's party, the AKP, used Istanbul's resources to award lucrative contracts to businesses close to the government and enrich themselves. The party denies the claim and says its aimed at undermining its record of delivering high growth and modern infrastructure.
But Imamoglu will need the support of district mayors and municipal assembly members to win approval of his projects and budget. Istanbul is a sprawling city, straddling Europe and Asia, with 39 districts. Each has its own mayor overseeing tasks that fall outside the metropolitan mayor's duties.
AKP controls 25 of Istanbul's 39 districts and a majority in the municipal assembly.
"The AKP now has two choices. One is, of course, working together with Imamoglu, taking advantage of the fact that (he) has publicly announced that he would like to work together with President Erdogan," said Ozgur Unluhisarcikli, Ankara office director of the German Marshall Fund.
The second option, Unluhisarcikli said, could be to undermine Imamoglu. With changes to local administration laws, Erdogan could "hollow out the powers of metropolitan mayors" by transferring some of the mayor's powers to district mayors or to the capital, Ankara.
RISKS TO POWER
Erdogan congratulated Imamoglu in a tweet but previously said on multiple occasions that a mayor from Turkey's political opposition would not be able to effectively run Istanbul with AKP controlling so many districts. The Turkish leader last week downplayed the importance of the mayor's office position, even though his own political career began as Istanbul mayor in 1994.
Erdogan has also hinted that a judicial process could be launched against Imamoglu for allegedly insulting the governor of northern Ordu province. Imamoglu denies calling the governor a "dog."
The Turkish government has a track record of seizing control of municipalities in the country's predominantly Kurdish southeast. After a failed July 2016 coup, the government appointed trustees to replace 95 local elected officials, alleging they had links to an outlawed Kurdish militant group.
The pro-Kurdish party regained many of those seats in March local elections. But the party says 68 officials were prevented from taking office in six municipalities and assemblies on the grounds they had been dismissed from public service under emergency decrees resulting from the failed coup.
Both Yildirim, in his concession speech, and Erdogan, in his tweets, said Sunday's results showed Turkish democracy and the "national will" at work in Istanbul.
In his victory speech to tens of thousands of supporters late Sunday, Imamoglu thanked Yildirim and Erdogan for congratulating him. He expressed hope for cross-party cooperation in Istanbul
"I am ready and willing to work with you," he said.
Follow Bilginsoy at http://twitter.com/zbilginsoy
Copyright 2019 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.