In a blow to Turkish President Recep Tayyip Erdogan, an opposition candidate declared victory in the Istanbul mayor’s race for a second time Sunday after the government-backed candidate conceded defeat in a high-stakes repeat election.

“Thank you, Istanbul,” former businessman Ekrem Imamoglu, 49, said to jubilant supporters after unofficial results showed he won a clear majority of the vote.

His opponent, former Turkish prime minister Binali Yildirim, conceded moments after early returns showed him trailing well behind Imamoglu, 54 per cent to 45 per cent.

The outcome means Turkey’s largest city won’t be governed by Erdogan’s party or its predecessor for the first time in 25 years.

Hundreds of opposition supporters erupted in mass celebration outside the headquarters of the Republican People’s Party, which backed Imamoglu, chanting “Mayor Again! Mayor Again!”

Imamoglu had narrowly won a previous mayoral election on March 31, but Erdogan’s Justice and Development Party challenged the vote. Turkey’s electoral board annulled the results after he had served 18 days in office and after weeks of partial recounts.

“You have protected the reputation of democracy in Turkey with the whole world watching,” Imamoglu, his voice hoarse after weeks of campaigning, told supporters.

Erdogan campaigned for Yildirim in Istanbul, where the president started his political career as mayor in the mid-1990s.

Erdogan’s party, AKP, also lost control of the capital city of Ankara in Turkey’s March local elections, which were held after the country slid into recession.

Election officials count ballot papers at a polling station in Istanbul on Sunday. (Emrah Gurel/Associated Press)

The voided vote in Istanbul had raised concerns domestically and abroad about the state of Turkish democracy and whether Erdogan’s party, which has been in control of the Turkish government since 2002, would accept any electoral loss.

Istanbul, a city of 15 million, draws millions of tourists each year and is Turkey’s commercial and cultural hub. Straddling Europe and Asia, Istanbul accounted for 31 per cent of Turkey’s GDP in 2017.