Turkey’s opposition appeared to be leading the vote for a closely watched and tightly contested race for Istanbul’s mayoralty, a major potential blow to the ruling Justice and Development Party (AKP) of President Recep Tayyip Erdogan.

The candidate of the secular liberal Republican People’s Party (CHP) Ekrem Imamoglu led his AKP opponent Binali Yildirim by nearly 28,000 votes, election commission chief Sadi Guven said in a televised appearance, warning that appeals were continuing.

The tense wait on Monday morning over the outcome of the vote for the mayoralty of Istanbul came after local elections that saw the AKP surrender the capital Ankara and suffer other losses to the opposition but retain its status as the country’s premier political organisation.

Mr Erdogan’s party and its predecessors have controlled both Istanbul and Ankara for a quarter century. Losing both would serve as a stinging rebuke against Mr Erdogan’s handling of the economy which suffered a financial meltdown last year.

“Erdogan’s brand for the last 17 years that he has been the invincible politician is going to be seriously undermined with these defeats,” said Soner Cagaptay, the director of the Turkish Research Program at the Washington Institute.

Election in Turkey: the reaction in pictures Show all 10 1 /10 Election in Turkey: the reaction in pictures Election in Turkey: the reaction in pictures Supporters of Mr Erdogan celebrate in Istanbul Getty Election in Turkey: the reaction in pictures A supporter of Mr Erdogan carries his portrait in front of a Turkish flag Reuters Election in Turkey: the reaction in pictures Following the election, a shipowner reads a newspaper that covers the result AP Election in Turkey: the reaction in pictures Supporters of Muharrem Ince, defeated opposition candidate, wait outside the Republican People's Party (CHP) headquarters following Mr Erdogan's claim of victory Getty Election in Turkey: the reaction in pictures People in an Instanbul barber shop listen to Muharrem Ince's speech following the results EPA Election in Turkey: the reaction in pictures Supporters of the pro-Kurdish People's Democratic Party (HDP) hold photos of its leader Selahatin Demirtas AFP/Getty Election in Turkey: the reaction in pictures Supporters of Mr Erdogan celebrate outside the AK party HQ in Ankara Getty Election in Turkey: the reaction in pictures Mr Erdogan waves to supporters in Ankara AP Election in Turkey: the reaction in pictures Mr Erdogan waves to supporters in Ankara AP Election in Turkey: the reaction in pictures Supporters of Mr Erdogan light flares as they celebrate the result AP

“The election is not for the national government but if he does lose Ankara and Istanbul, it would be a bit awkward for him because he would be Turkey’s president in charge but would not be controlling his two largest cities.”

Mr Erdogan, speaking at a press conference late Sunday, sought to put the numbers in a positive light. “Results show that as the AKP, we emerged from this election as the top party by far, just as has been the case since the 3 November 2002 election,” he said, but acknowledged the possibility of losing Istanbul.

On Monday, AKP stalwarts came out swinging. Party leaders announced plans to contest the Istanbul vote. AKP Istanbul provincial chief Bayram Senocak alleged “irregularities," and claimed there were over 300,000 "invalid" vote.

“According to the figures we have, the result in Istanbul is the AKP and Binali Yildirim,” he said.

On social media accounts, supporters of the party alleged cheating by the opposition in both Istanbul and Ankara, without citing evidence.

"There is no such thing as Ekrem Imamoglu winning in Istanbul," one Twitter user with 75,000 followers said. "There are 207,000 AKP votes that have not been counted."

Ekrem Imamoglu, opposition candidate for Istanbul mayor (AP)

At polling stations on Sunday, where a spirit of affability mostly reigned among voters, the economy weighed heavily.

“Unaffordable living costs have to come to an end, be resolved,” said Zehra Yilmaz, 58, a housewife, criticising the AKP.

“This is more than a local election, it carries a message that I’m sure will come out strong and reach the right people in the end.”

Opposition candidate Mansur Yavus, mayor-elect of Ankara (AP)

Mr Erdogan himself had framed municipal elections often driven by local concerns and personalities as a referendum on him and his party, often taking the lead during campaign rallies for allied candidates. He and surrogates fought fiercely, accusing the opposition of being in league with “terrorists.”

State television was accused of giving one-sided coverage favourable to the AKP, while newspapers and preachers close to the government urged citizens to vote.

“Local elections today in Turkey look like general elections,” said Nizamettin, a resident of the same Kasimpasa neighbourhood where Mr Erdogan grew up. “I voted for Erdogan. He is the only one to fix things in this country.”

The election on Sunday saw voters come out in droves to show loyalty to the AKP, which has dominated the country’s political life for nearly 17 years, but also to punish it for the country’s economic troubles and its backsliding on democracy and civil liberties.

“I voted for the CHP,” said Deniz, an art director in Besiktas, another neighbourhood of Istanbul.

Recep Tayyip Erdogan shows snippets from New Zealand terrorist videos at campaign rallies

“I wasn’t planning to vote but I changed my mind after witnessing verbal attacks against Ekrem Imamoglu on TV. Some of my friends changed their minds too – just like me. ‘Why am I going to vote?’ That is what I was thinking on my way here but I voted anyway.”

Turnout was at 84.5 per cent of registered voters, and passions were high. In the eastern Turkish province of Malatya on Sunday, two poll workers for the Islamist opposition Saadet Party were shot dead in a dispute with a man described as the nephew of a local AKP candidate.

The unusually high stakes over the vote made the battle for Istanbul, where Mr Erdogan grew up and served as mayor during the 1990s, especially crucial. With 16 million or so inhabitants spread along the European and Asian sides of the Bosphorus Strait, it is larger and more complex than some countries.

But other election contests around the country also caught the attention of local and international observers. For the first time in Turkey’s history, a communist party member won a municipal election. Lab technician Fatih Mehmet Macoglu, won the mayoralty of Tunceli, or Dersim, a heavily Kurdish Zaza district in far eastern Turkey.

Opposition rallies after municipal elections in Diyarbakir, Turkey. (AP)

Many election watchers were surprised that the southeastern province Sirnak, which had been a stronghold of the Kurdish-led leftist People’s Democratic Party (HDP) was won by the AKP, with as-yet unsubstantiated accusations of ballot-stuffing surfacing. But elsewhere in the heavily Kurdish southwest, the HDP managed to wrest back control of local posts where the government over the last few years has removed officials and placed pro-AKP trustees.