Turkish President, Recep Tayyip Erdogan has suffered a major political blow as his candidate for Istanbul mayor was defeated in a repeat election.

The election is a repeat of a previous vote in March which was annulled after the opposition candidate won a slim victory.

 Erdogan tweeted to congratulate opposition candidate, Ekrem Imamoglu who led with 54% in early results compared to 45% won by the Turkish president’s chosen candidate, Binali Yildirim.

“I congratulate Ekrem Imamoglu who won the election according to unofficial results. As before, we will walk to our goals in unity and togetherness, without compromising democracy, the rule of law, peace, prosperity and stability of our country,” Erdogan said.

Mr Imamoglu previously won the mayoral election on March 31 but Erdogan’s ruling party challenged the vote and the results were annulled after weeks of partial recounts.

The opposition candidate was then stripped of the post of mayor after serving just 18 days and another election was ordered.

Concerns were raised about the voided vote both in Turkey and abroad as many questioned the country’s democracy and whether Erdogan’s party would ever concede defeat.

In a speech declaring his victory for the second time on Sunday, Mr. Imamoglu thanked his supporters as they chanted “Mayor again! Mayor again!”

“You have protected the reputation of democracy in Turkey with the whole world watching,” he told the crowd gathered outside the headquarters of the Republican People’s Party.

The results mean Turkey’s largest city will not be governed by Erdogan’s AKP party for the first time in a quarter of a century.

Turkish media also puts Mr. Imamoglu’s lead on Sunday at about 700,000 votes, far more than the roughly 13,000 vote margin in March.

The ruling party also lost control of Ankara, Turkey’s capital city following the local elections in March. However, the party still controls 25 of Istanbul’s 39 districts and holds a majority in the municipal assembly.