The party of Turkey's President Recep Tayyip Erdogan has lost control of the capital, Ankara, in local elections, in a blow to his 16-year rule.
The main opposition is also slightly ahead in the contest for mayor of Istanbul, figures published by the state-run Anadolu news agency suggest.
But the president's AKP party is challenging the result in both cities.
Municipal elections were held across the nation on Sunday and an AKP-led alliance won more than 51% of the vote.
The elections, considered a verdict on Mr Erdogan's rule, have been taking place during an economic downturn.
The currency, the lira, has been losing value recently and the economy went into recession in the last three months of 2018.
What has the ruling party been saying?
The AKP - or Justice and Development Party - alleges "invalid votes and irregularities in most of the 12,158 polling stations in Ankara".
Its general secretary, Fatih Sahin, said on Twitter: "We will use our legal rights to the fullest, and we will not allow the will of our citizens to be altered in Ankara."
The AKP says it will also challenge the result in Istanbul - the largest city - and the eastern province of Igdir.
Commenting on the results in a speech on Sunday, Mr Erdogan looked ahead to national elections in 2023: "We have a long period ahead where we will carry out economic reforms without compromising on the rules of the free-market economy.
"If there are any shortcomings, it is our duty to correct them," he said.
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 opposition Republican People's Party (CHP) candidate Mansur Yavas won in Ankara, officials said. With almost all votes counted, he was on nearly 51% and the AKP's Mehmet Ozhaseki had won the support of just over 47%.
Both CHP and the AKP claim victory in Istanbul, which has been in the hands of parties linked to Mr Erdogan since 1994, when he was elected the city's mayor.
The election commission said the CHP's Ekrem Imamoglu was leading there by less than 0.5%, but that the results of more than 80 ballot boxes were being challenged. Results carried by Anadolu put the margin even narrower, at less than 0.25%.
The AKP had been saying its 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.
The third largest city, Izmir, went to the CHP.
"The people have voted in favour of democracy. They have chosen democracy," CHP leader Kemal Kilicdaroglu said.
Image of invincibility broken
Analysis by Mark Lowen, BBC Turkey correspondent
Electoral figures show Istanbul falling to the opposition by a whisker, although the ruling party has challenged the result and refused to concede. Ekrem Imamoglu, who's already changed his Twitter profile to "mayor of Istanbul", has vowed to serve all sides - those who voted for and against him - without discrimination.
President Erdogan's setbacks deepened elsewhere, losing the capital, Ankara, and several other cities as his conservative voters punished him for an economic crisis.
Local elections are important here. Parties build their base at grassroots level; Mr Erdogan himself gained power after being mayor of Istanbul.
He turned this poll into, in effect, a referendum on himself. Now his image of invincibility has been broken and 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's beatable.
How was the campaign?
This was the first municipal vote since Mr Erdogan assumed sweeping executive powers through last year's presidential election.
The AKP, with its roots in political Islam, 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 the country 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.