Analysts said the re-run was a lose-lose proposition for Tayyip Erdogan.

Turkish President Recep Tayyip Erdogan suffered a bruising defeat on Sunday as the opposition candidate in Istanbul's controversial mayoral election re-run romped to victory.

Ekrem Imamoglu had won the first mayoral vote in March by a slim margin of 13,000 votes over Erdogan's chosen candidate, Binali Yildirim, who contested the results and a new election was ordered.

After being stripped of that victory over disputed claims of fraud, Imamoglu vowed a "battle for democracy" that turned him into a household name.

With some 99 percent of ballots counted by Sunday night, Imamoglu was winning by more than 775,000 votes -- 54 percent, with Yildirim on 45 percent, the Anadolu state news agency reported.

"It was not a single group or party, but the whole of Istanbul and Turkey that won this election," Imamoglu said in his victory speech as scenes of his ecstatic supporters broke out across the city of 15 million.

"Mr President, I am ready to work in harmony with you. I convey from here my request to meet with you in the shortest time possible," he added.

Critics had accused Erdogan of refusing to give up control of Istanbul, Turkey's economic powerhouse and a crucial source of patronage for Islamic conservatives since he won the mayorship himself a quarter of a century ago.

But the Turkish leader conceded defeat this time, writing on Twitter: "I congratulate Ekrem Imamoglu who has won the election based on preliminary results."

'Everything will be fine'

Imamoglu, a little-known district mayor at the start of the year, galvanised voters with a relentlessly positive campaign under the slogan, "Everything will be fine".

That was in stark contrast to the usual aggressive name-calling of Turkish politics and struck a chord with voters.

"Our happiness has just started. We will celebrate till morning. We have been waiting 17 years (for a win). We believed in him and he believed in us," said 29-year-old Imamoglu supporter Eda Alday, as cars honked and fans waved Turkish flags in the street.

Imamoglu faced the juggernaut of Erdogan's ruling AKP party, which has been in power in Turkey since 2002 and remains the most popular political force nationwide.

AKP candidate Yildirim, a mild-mannered Erdogan loyalist who oversaw several huge transport projects and served as prime minister, this time quickly conceded defeat.

He had already struck a conciliatory tone earlier on Sunday, saying "if we have wronged, knowingly or unknowingly, one of our fellow Istanbulites or our challengers, if we have done something unjust, I ask for your forgiveness."

'Colossal defeat'

As the city of Istanbul erupted in spontaneous celebrations over Imamoglu's victory, commentators noted that this was more than a municipal vote -- Erdogan himself once said: "Whoever wins Istanbul, wins Turkey."

"It's a colossal defeat for Yildirim but also Erdogan. His gamble (in calling for a replay of the election) backfired," Berk Esen, assistant professor of international relations at Ankara's Bilkent University, told AFP.

It comes as an economic slump and rising prices have dented the president's reputation for economic stewardship, with the AKP also losing the capital Ankara in March.

"The AKP elites will probably try to de-emphasise the election and act like it's no big deal," said Esen.

Erdogan has already played down the importance of the re-run, saying last week that the choice of mayor was "only a change in the shop window" since the AKP controls almost two-thirds of the city's districts.

But it was a bitter loss for AKP voters, with some in tears at the party headquarters on Sunday night.

"Let them rejoice while they can, they will see...," vowed retired shopkeeper Mehmet.

For many conservatives, Erdogan remains a hero who has brought prosperity and fiercely defended the country's interests since taking over in 2003.