ANKARA  -  Turkish president Recep Tayyip Erdoğan has claimed a decisive victory for his ruling party in local elections viewed as a crucial test of his leadership, even as initial results pointed to wins for the opposition in Istanbul and Ankara.

State media reported on Sunday that Erdoğan’s Justice and Development party (AKP) had lost control of Ankara to opposition bloc mayoral candidate Mansur Yavas – a significant blow to the AKP, ending 25 years of the Islamist party’s dominance over the capital.

In Istanbul, main opposition candidate Ekrem İmamoğlu said in the early hours of Monday that he had won by nearly 28,000 votes over AKP candidate and former prime minister Binali Yildirim.

But minutes later, the AKP provincial head in Istanbul said Yildirim had won by around 4,000 votes. Recep Tayyip Erdoğan addresses supporters from the balcony of AK party headquarters in Ankara on Sunday. Several recounts are expected overnight and on Monday in what should have been routine municipality elections across Turkey which instead morphed into a referendum on Erdoğan’s 16-year rule as the country’s economic woes began to bite into his popular support.

As the country digested the results on Monday morning, the credit ratings agency Moody’s warned that Erdoğan’s response to the financial crisis risked renewed capital flight. It also called into question the independence of the country’s central bank, Moody’s said. Berat Albayrak, Turkey’s financial minister and Erdoğan’s son in law, has promised financial reforms would be unveiled shortly after the local elections.

The initial election results mark the first significant defeat for the ruling AKP since it first took power in 2002. “Every gain and every loss is the will of our people and also a requirement of democracy that should be acknowledged,” Erdoğan said in Istanbul before flying to Ankara. “We will admit we won the hearts of our people in areas that we won and we will admit we were not successful enough in areas we lost.”

AKP took nearly 45% of the votes after 90% of the more than 194,000 ballot boxes were counted, according to state broadcaster TRT. The secular main opposition party, CHP, had 30% overall.

The pro-Kurdish People’s Democratic Party (HDP) managed to regain several seats across the mainly Kurdish southeast of the country, where the government has replaced elected mayors with government-appointed trustees in the past after alleging that the ousted officials had links to the outlawed militant PKK, or Kurdistan Workers’ Party. It lost control of two cities where government loyal security services have been posted to the AKP. The elections were also marked by scattered episodes of violence. At least four people were killed in south and east Turkey and dozens injured in election-related clashes in the Kurdish-majority city Diyarbakir. In Istanbul, police said one person was stabbed in a 15-person brawl that broke out between candidates in Kadıköy district.

Erdoğan’s time in office has been distinguished by consistently strong economic growth, but last year’s currency crisis officially triggered a recession last month. Inflation is hovering at about 20%, sending the cost of living soaring for working-class AKP voters.

Although the president was not on the ballot himself, he has campaigned tirelessly over the last few weeks in an attempt to draw attention away from the economy. He framed the local elections as a matter of “national survival”, accusing opposition parties of links to terrorism and blaming inflation on foreign powers seeking to undermine the country.

Sunday’s elections also marked the first ballot-box test for Erdoğan since he won re-election last year under a new system of government that gave the presidency expanded powers.

Opposition hopes that dissatisfaction at inflation and rising unemployment would be enough to dissuade working-class AKP voters from turning up to vote appeared to be well-founded. Polling stations across Istanbul on Sunday had been quieter than expected, several vote monitors said.

“We keep showing up to vote. I think we’re a bit bored of it,” said 74-year-old Engin Yorulmaz, from Istanbul’s Trakya district. “But maybe we’re bored of change as well.”

Critics have said Sunday’s elections were not fought fairly, with several HDP leaders in the south-east of the country arrested on terror charges in the runup to voting day. Largely pro-government media coverage has also put opposition parties at a campaigning disadvantage.

On Sunday, the Felicity party leader, Temel Karamollaoğlu, tweeted that two party members, a polling station volunteer and a party observer, were killed in eastern Malatya province by a relative of an AKP candidate.

The killings were not caused by “simple animosity”, but happened when the volunteers tried to enforce the law requiring ballots to be marked in private voting booths instead of out in the open, Karamollaoğlu said.

Turkey’s official Anadolu news agency reported the deaths were the result of a brawl. Speaking after he voted in Istanbul, Erdoğan said he was saddened by the deaths but did not want them to lead to “a questioning or a judgment between political parties”.

Two more people were reported dead in Gaziantep, in south Turkey, on Sunday evening, after supporters of two rival candidates opened fire on each other. Two others were wounded in the shooting.

For AKP supporters, Erdoğan’s strong leadership is exactly what will help Turkey weather its financial crisis. “Before, there were no services, no rubbish collection, we didn’t have insurance to visit the hospital. Now life is good for us,” said Havva Guney, 56.

“Some people say there’s no money left but the markets are still open. People still leave with bags of food.”