Ankara (AFP) - President Recep Tayyip Erdogan on Friday accused the US and Europe of "meddling" in Turkey's affairs after statements from Western allies following his ruling party's challenge to local election results.

Erdogan and his AKP suffered an upset in Sunday's ballot when results showed the party lost the capital Ankara and was narrowly defeated in Istanbul, the country's largest city and economic hub.

While the United States has called on Turkey to accept the results, the European Union urged Ankara to allow elected officials to "exercise their mandate freely".

But Erdogan rejected the remarks and told the US and Europe to "know your place".

"America and Europe are... meddling in Turkey's internal affairs," Erdogan said in his first direct remarks to journalists since Monday.

"Turkey gave a democracy lesson to the whole world," he added.

US State Department spokesman Robert Palladino said "free and fair elections are essential for any democracy, and this means acceptance of legitimate election results are essential" during a briefing on Tuesday.

EU spokeswoman Maja Kocijancic on Monday said Brussels expected elected local representatives to be able "to exercise their mandate freely and in line with the principles of the Council of Europe (rights group) to which Turkey is of course party."

Andrew Dawson, head of a delegation from the Council of Europe in Turkey to observe the vote, said on Monday his team was "not fully convinced that Turkey currently has the free and fair electoral environment which is necessary for genuinely democratic elections in line with European values and principles."

- 'Over by weekend' -

The two rival Istanbul candidates both declared victory soon after the vote when initial results showed them in a dead heat. The AKP appealed after electoral authorities later said the opposition's Ekrem Imamoglu had taken a very slim lead.

The Turkish head of state hit out at American and European criticism over his party's push for appeals.

Story continues

"In your own countries, you have launched appeals."

Erdogan fought hard before the vote, holding rallies across Turkey where he described the election of mayors and district councils as a battle for the nation's survival.

But voters, concerned with the rising cost of living, double-digit inflation and unemployment, opted for the opposition in the country's two biggest cities.

CHP opposition candidate Imamoglu told Fox Haber broadcaster earlier the difference between himself and the AKP's candidate, Binali Yildirim, was now 18,742 votes.

He said nearly 120,000 previously annulled votes had been recounted in 17 Istanbul districts, with 2,184 extra votes being allowed for Yildirim, and 785 for himself.

Imamoglu estimated the final difference could finish between 18,000 to 20,000 once counting ended, probably by Sunday. Nearly 200,000 more votes still need to be recounted, he said.

"It should be over by the end of the weekend," he told the station.

The AKP has said the recount will show its candidate won.

The Pro-Kurdish Peoples' Democratic Party (HDP), meanwhile, said none of its appeals for recounts had been accepted by election boards, even when candidates had won by only a few votes.

The HDP supported the CHP -- to protest the government's "harsh policies" in Kurdish-majority cities -- by not fielding its own candidates and splitting the anti-Erdogan vote in western Turkey.

It said it had challenged the rulings and would "appeal to the Supreme Elections Board if provincial boards, too, reject our appeals".

- Ankara recount -

Election authorities are providing ongoing recount details to observers from both parties to ensure transparency. Political parties have until April 10 to challenge the results.

A defeat in Istanbul would be especially sensitive for Erdogan who made his political career as mayor of the city. The AKP or its predecessors had held both cities since 1994.

Opposition candidate Mansur Yavas of the CHP beat the AKP's Mehmet Ozhaseki in Ankara, according to preliminary results.

AKP officials have said they believe there was a huge discrepancy in both cities between ballots cast at polling stations and the actual data sent to election authorities.

Erdogan on Friday said his party won 24 out of 39 districts of Istanbul, but said: "The final decision will be taken by the Supreme Election Board."