President Recep Tayyip Erdogan lost the mayoral race in the capital Ankara to Turkey's secular opposition as the country staged pivotal local elections. Erdogan declared victory in the elections on Sunday night but the opposition's success in Ankara and elsewhere dealt a significant blow to his party's dominance. Votes are still being counted in Istanbul where the race for mayor is said to be 'too close to call'. The candidate of the main opposition Republican People's Party (CHP) in Turkey's largest city Istanbul is ahead in the vote count against the candidate of President Tayyip Erdogan's AK Party (AKP), the head of the country's High Election Board said on Monday. Erdogan, prepares to cast his ballot during the local elections in Istanbul on Sunday surrounded by members of his party Ekrem Imamoglu, the mayoral candidate for Istanbul representing the Republican People's Party (CHP) looks set to win the city according to the latest vote counts Sadi Guven told reporters that the CHP's Ekrem Imamoglu had 4,159,650 votes and the AKP's Binali Yildirim had 4,131,761 votes. He did not specify which percentage of votes had been counted. Sunday's local elections were widely seen as a test of support for Erdogan as the nation of 81 million people faces a daunting economic recession with double-digit inflation, rising food prices and high unemployment. The main opposition party winning the race for metropolitan mayor in Ankara marks a symbolic shift. The capital city was held by Erdogan's Justice and Development Party, the AKP, and its Islamic-oriented predecessor for 25 years. More than 57 million voters were eligible to take part in choosing the mayors of 30 major cities, 51 provincial capitals and 922 districts in Turkey. In big cities, voters cast four ballots, for metropolitan mayor, district mayor, the municipal assembly and a neighborhood administrator. According to unofficial results reported by the state-run Anadolu news agency, Mansur Yavas, the candidate of the secular Republican People's Party, or CHP, won the top post in Ankara. The AKP still holds a majority of Ankara's 25 districts. Election officials start counting ballots at a polling station during the local elections in Ankara which were lost by Erdogan's party An official inspects one of the ballot papers during counting for the local elections in Ankara on Sunday evening Ballots are transported after being counted at a polling station during the local elections in Diyarbakir The government had led a hostile campaign against Yavas and his party, accusing him of forgery and tax evasion. But Erdogan still came out on top as the ruling party and an allied nationalist party won more than half of the votes across Turkey. Speaking from the balcony of party headquarters in Ankara early Monday, where he has for years declared victory, Erdogan said he and his allies had come out first. Admitting setbacks, he said the party would work to understand and fix where they failed. Ballot counts were still underway Monday morning in Turkey's largest city and commercial hub, Istanbul. Both candidates -Ekrem Imamoglu for the CHP and former Prime Minister Binali Yildirim for the AKP- claimed they had won. The opposition accused Anadolu of bias, and the agency's unofficial results for Istanbul were stuck at 98 percent of votes counted for hours. The private Demiroren news agency showed Imamoglu leading Monday. The mayor's seat in the city of 15 million is important. Erdogan's own ascent to power began there in 1994. Supporters of President Recep Tayyip Erdogan wave flags in Istanbul after he declared victory in the elections Erdogan's AK Party lost crucial votes in Ankara and other cities as it saw its stronghold on many mayoral seats broken Erdogan addressing a huge crowd from the balcony of AK Party's Headquarters in Ankara on Sunday In predominantly Kurdish provinces, a pro-Kurdish party won seven top mayoral seats but lost some strongholds, including southeastern Sirnak province, to the ruling party. Since 2016, the government has replaced 95 elected municipal, provincial or district mayors with government-appointed trustees in a crackdown on the Peoples' Democratic Party, or HDP, for alleged links to outlawed Kurdish militants. The HDP had vowed to win them back. Amid a media blackout and despite significant losses in the region, it regained the municipal mayor in the Kurds' symbolic capital, Diyarbakir. Government officials have previously threatened they won't recognize results if HDP candidates with "terror" links win. The secular opposition also made significant gains in provinces along the Mediterranean, including taking Adana from the nationalists and the resort-town of Antalya from the ruling party. It also retained its hold over Izmir, Turkey's third largest city, located on the Aegean Sea. If Imamoglu wins in Istanbul, the opposition will administer the three most populous cities. A strategic decision by the pro-Kurdish party to sit out critical races in major cities may have contributed to the secular party's increase in votes. Turkey will also have its first communist mayor, in the eastern province of Tunceli. People's Democratic Party supporters (HDP) celebrate after the announcement of preliminary results of the local elections, in Diyarbakir Supporters of Justice and development party (AKP) wave, from a car, a flag picturing Turkish president Recep Tayyip ErdoganAll rights reserved for this news site dailymail and under his responsibility