President Recep Tayyip Erdogan and his Justice and Development Party (AKP) said they secured most mayoral positions nationwide in Sunday's municipal elections. But the AKP suffered major losses, as the main opposition's candidates eked out a win in the capital, Ankara, and pulled ahead in Istanbul, according to results released Monday.

In Istanbul, the election board said the opposition's mayoral candidate Ekrem Imamoglu was ahead by a slim margin of 48.8 percent against 48.5 percent for the ruling party's candidate, former Prime Minister Binali Yildirim.

Erdogan's AKP claimed victory in Turkey's largest city and economic powerhouse, after claiming there were voting irregularities. The results appeared to set the stage for a recount and showdown through the election commission over who would take Istanbul.

State media reported that Erdogan's Islamic-based party won over 44 percent of the votes across the country, while the main opposition party, the Republican People's Party (CHP), bagged over 30 percent of the vote.

More than 57 million voters were eligible to take part in electing the mayors for 30 major cities, 51 provincial capitals and 922 districts across Turkey. Turnout was a high 85 percent.

The elections were seen as a crucial test for Erdogan's popularity.

Ekrem Imamoglu said it was "very clear" the opposition won in Istanbul

A 'referendum' on Erdogan's leadership

President Erdogan's past electoral successes have been based on a strong economy, but with a weakening currency, increasing inflation and soaring food prices, his conservative AKP party faced a tough challenge for key mayoral seats.

On Sunday night, the Turkish president told supporters that his political party remained the "No.1 party" in the country, but he also acknowledged possible shortcomings in the municipal elections.

"If there are any shortcomings, it is our duty to correct them," Erdogan told a crowd gathered in Ankara.

Read more: Recep Tayyip Erdogan: The sultan of 21st-century Turkey

Violence and unfair advantage

Voting was marred by election violence that killed at least two people and wounded dozens across the country.

Erdogan expressed his sorrow over the deaths and said he did not want them to become a cause for "a questioning or a judgment between political parties."

Erdogan used the election campaign to malign the opposition parties, portraying them as enemies of the country. He also said that Turkey's economic woes were a result of attacks by enemies at home and abroad.

The opposition pro-Kurdish Peoples' Democratic Party (HDP) did not field candidates in several major cities outside their stronghold in the southeast, giving the opposition a boost as some Kurdish voters appeared to strategically vote for CHP candidates in Istanbul and Ankara.

Over the past several years, dozens of HDP politicians have been jailed on terror charges and the government replaced local mayors and officials with appointees.

The HDP accused the government of trying "to paralyze the HDP organizationally, and render the playing field even more uneven."

Independent observers say that opposition parties campaigned at a disadvantage because Erdogan's daily rallies dominated the media, which are either pro-government or controlled by the president's supporters.

Since a botched military coup in 2015, President Erdogan has tightened his grip on power and has launched a crackdown on rights groups, civil society and opposition activists.

German reaction

Germany's political parties reacted to the news. The CDU's Norbert Röttgen told DW: "There is an opportunity for voters to oust the AKP and to vote for others. This is a good signal and a sign that there is something that you can call democracy. Under restrictive conditions, however, it is possible to not vote for the AKP and to bring about victories for opposition parties."

For the SPD, Nils Schmid said: "From the viewpoint of the ruling AKP, it is certainly a disappointment. From the point of view of the opposition, it is a success ... which gives me hope for political pluralism in Turkey. The opposition has succeeded in achieving this success, which is a good sign of democracy in Turkey."

The Greens foreign policy spokesman Omid Nouripour told DW: "It wasn't a landslide defeat for him. But it is obvious that the party has lost supporters. And it is very significant that the AKP has lost especially in the urban centers which are usually, traditionally, the economic powerhouses of Turkey. This is the place where people are starting to doubt the AKP's ability to resolve the economic problems of the country."

Watch video 04:58 Share Turkey: Construction boom collapses Send Facebook google+ Whatsapp Tumblr linkedin stumble Digg reddit Newsvine Permalink https://p.dw.com/p/3EnT2 Turkey: Construction boom collapses on AKP

cw,rs, jcg,shs/jil (AP, Reuters, AFP)

Every evening, DW's editors send out a selection of the day's hard news and quality feature journalism. You can sign up to receive it directly here.