Turkish President Recep Tayyip Erdogan's AK Party is contesting the slim opposition victory in the country's biggest city, Istanbul.
Sunday's municipal elections also delivered opposition CHP wins in the capital, Ankara, and in Izmir.
The AKP alleges irregularities, and is challenging the results in every Istanbul district. Officially the CHP is ahead by 25,000 votes in the city.
Giant AKP victory posters have gone up in Istanbul.
The secularist CHP (Republican People's Party) condemned that move, accusing the Islamist-rooted AKP of trying to steal the result.
The AKP is also contesting the CHP victory in Ankara.
Parts of the largely government-controlled media are reporting allegations that stolen ballots hindered the party's performance.
In the past, Turkish opposition parties have failed to mount a successful challenge to election results.
Is it a big setback for Erdogan?
Nationwide, an AKP-led alliance won 51.6% of the vote.
But Istanbul has been in the hands of parties linked to Mr Erdogan since 1994, when he was elected the city's mayor.
The BBC's Mark Lowen in Istanbul says losing major cities is a significant blow to Mr Erdogan, breaking his image of invincibility.
The elections, considered a verdict on Mr Erdogan's 16-year rule, were held during an economic downturn.
The Turkish lira has been losing value recently and the economy went into recession in the last three months of 2018. Inflation jumped to 20% last year and unemployment has risen too.
Our correspondent says Mr Erdogan turned this poll into, in effect, a referendum on himself. But now an opposition long seen as hopelessly divided has got a new lease of life.
Swathes of Turkey still adore Mr Erdogan, but the half of the country that detests its polarising president are starting to believe he is beatable, Mark Lowen says.
What are the results?
More than 57 million people in the country were registered to vote for mayors and councillors. Turnout was high at just under 85%.
The results, reported by the state-run Anadolu news agency, show:
- Istanbul - 48.8% CHP, 48.5% AKP
- Ankara - 50.9% CHP, 47% AKP
- Izmir - 58% CHP, 38.6% AKP
The AKP had been saying its Istanbul candidate, former Prime Minister Binali Yildirim, was ahead by 4,000 votes. He later conceded his opponent had a narrow lead, only for the AKP to again claim victory.
What happened during the campaign?
This was the first municipal vote since Mr Erdogan assumed sweeping executive powers through last year's presidential election.
The AKP launched a sweeping purge after a failed military coup in 2016. The government has detained more than 77,000 people and suspended or sacked about 150,000 civil servants and military personnel.
The AKP has won every election since coming to power in 2002.
Mr Erdogan, whose two-month campaign included 100 rallies, said the poll was about the "survival" of Turkey and his party.
With most media either pro-government or controlled by Mr Erdogan's supporters, critics believe opposition parties campaigned at a disadvantage. Mr Erdogan's rallies dominated TV coverage.
The opposition pro-Kurdish Peoples' Democratic Party (HDP) said the elections were unfair and refused to put forward candidates in several cities.
Some of its leaders have been jailed on terrorism charges, accusations they reject.