The opposition mayoral candidate for Istanbul has won Turkey's largest city, although the alliance led by President Recep Tayyip Erdogan’s Justice and Development Party won the most votes nationwide. The capital Ankara, the second-largest city, also fell to the opposition Republican People’s Party (CHP), as did the fifth-largest, Adana, and the Mediterranean city of Antalya. Mr Erdogan's AKP said it would challenge the results in Ankara and Istanbul, and other areas. General secretary Fatih Sahin said the party’s objections over invalid ballots and other irregularities in Ankara would be lodged with the election board, which would accept appeals up to 3pm on Tuesday. Mr Sahin said the party would also file objections over the result in Istanbul. The AKP's Istanbul candidate, former prime minister Binali Yildirim, said there were more than 300,000 invalid votes that could be recounted. The loss of Ankara and Istanbul – particularly the latter, where Mr Erdogan grew up and launched his political career – was a serious blow to the AKP’s prestige. But winning more than 50 per cent of the overall vote with its ally, the Nationalist Movement Party (MHP), gave the president some grounds for claiming success. However, the AKP is calling for recounts across the country, including in the Black Sea province of Bartin, where it plans to challenge the win of its MHP partner. Appeals against initial counts could last until the middle of next week as the complaints can be taken all the way to the Supreme Election Board if they are turned down at district and provincial level. Mr Erdogan travelled to Istanbul on Monday and met Mr Yildirim, the provincial governor and the chief of police, Demiroren news agency reported. The vote count in Istanbul, which, like Ankara, had been governed by the AKP or its predecessors for 25 years, was the most dramatic. It was not until 9am on Monday that the head of the electoral authority declared that the CHP’s Ekrem Imamoglu led Mr Yildirim by about 28,000 votes with more than 99 per cent of the ballots counted. Mr Imamoglu, a relative unknown until he was selected to run for Istanbul against his heavyweight opponent, made a dozen appearances throughout the night. The state-run Anadolu news agency, the sole distributor of election data, stopped updating the Istanbul count shortly after Mr Yildirim declared victory at 11.30pm on Sunday. To the surprise of many, the former premier announced his win with Mr Imamoglu gaining ground and less than 4,500 votes behind. Anadolu then halted the release of voting figures for several hours, sparking indignation among the opposition. “This is a democratic process and history will judge you,” Mr Imamoglu said about the agency. “You are responsible to society, to our nation.” It was not until just after 1pm that Anadolu reported the CHP leading in Istanbul. Turkey faces an economic crisis that has led to inflation of 20 per cent and unemployment rising to double figures. “Since 1994, local elections for Istanbul created new political leaders,” said Bulent Gultekin, a former central bank governor. “Also since then, no leader or party survived an economic crisis in Turkey. Erdogan will not be an exception.” In Ankara, CHP candidate Mansur Yavas beat AKP rival Mehmet Ozhaseki by a margin of nearly 4 points, or 125,000 votes. “This is amazing. We run the three largest cities in Turkey and have shown the world that democracy is not dead in this country,” said CHP supporter Alp Topaloglu after a night celebrating outside the party’s Ankara offices. The CHP retained control of Turkey’s third city Izmir by a comfortable margin. Mr Ozhaseki said it was “very clear, very obvious” that there were thousands of errors made in the ballot boxes in Ankara. Sunday’s vote was marred by claims of irregularities across the country and four people were killed in election-related violence. In an election night speech, Mr Erdogan pointed to the AKP’s victory in 16 of Turkey’s 30 metropolitan cities and pledged to initiate a reform programme before presidential and parliamentary elections in 2023. In a vote with 84 per cent turnout that was widely seen as a litmus test for the government, AKP supporters pointed to successes against the Kurdish Peoples’ Democratic Party (HDP) in the south-east. Mr Erdogan said Kurds had shown “they will not yield to a terrorist group or to those who have come out of the woodwork with the backing of a terrorist group”. The government regards the HDP as being aligned to the outlawed Kurdistan Workers’ Party (PKK) and has imprisoned thousands of its supporters and officials since 2016. Others said the HDP had been key to opposition wins in other cities. Although not part of the official opposition coalition, the HDP did not field candidates in those cities and encouraged supporters to vote for the CHP or its ally, the Iyi Party. “Its support to opposition candidates in Istanbul, Ankara, Adana, Mersin, Antalya has been decisive in the AKP losing those cities,” said Mutlu Civiroglu, a Kurdish affairs analyst in Washington. The election also resulted in a communist city mayor taking office for the first time as Fatih Mehmet Macoglu won the eastern city of Tunceli. Timothy Ash, an economist with BlueBay Asset Management in London, said the “truly remarkable” campaign, in which the AKP dominated the media and focused on threats to Turkey, had left democracy the “big winner”. “Turkey still faces big challenges in terms of the state of relations with key western allies and the parlous state of the economy,” Mr Ash said.