Turkish President Recep Tayyip Erdogan declared victory in municipal elections but the opposition's success in key cities dealt a significant blow to his party's dominance.
The ruling party lost the capital Ankara and the race for Istanbul mayor was still too close to call early on Monday.
Prime Minister Imran Khan congratulated Erdogan on "another important victory" in the Turkish local elections. "The people of Pakistan wish him many more successes," said the premier on Twitter.
Sunday's local elections were widely seen as a test of support for Erdogan as the nation of 81 million people faces a daunting economic recession with double-digit inflation, rising food prices and high unemployment.
The main opposition party winning the race for metropolitan mayor in Ankara marks a symbolic shift. The capital city was held by Erdogan's Justice and Development Party, the AKP, and its Islamic-oriented predecessor for 25 years.
More than 57m voters were eligible to take part in choosing the mayors of 30 major cities, 51 provincial capitals and 922 districts in Turkey.
In big cities, voters cast four ballots for parties running for metropolitan mayor, district mayor, the municipal assembly as well as a neighborhood administrator.
According to unofficial results by state-run Anadolu news agency, Mansur Yavas, the secular Republican People's Party or CHP's candidate, has won the top post in Ankara. The AKP still holds a majority of Ankara's 25 districts.
The government had led a hostile campaign against Yavas and his party, accusing him of forgery and tax evasion.
But Erdogan still came out on top as the ruling party and its allied nationalist party won more than half of votes across Turkey.
Speaking from the balcony of party headquarters in Ankara early on Monday where he has for years declared victory, Erdogan said he and his allies had come out first.
Admitting setbacks, he said the party would work to understand and fix where they failed.
Ballot counts were still underway on Monday morning in Turkey's largest city and commercial hub Istanbul. Both candidates Ekrem Imamoglu for the CHP and former Prime Minister Binali Yildirim for the AKP claimed they won.
The mayor's seat in the city of 15m is important. Erdogan's own ascent to power began there in 1994.
In predominantly Kurdish provinces, a pro-Kurdish party won seven municipal mayor seats but lost some strongholds to the ruling party.
The government had replaced elected mayors with government-appointed trustees in a crackdown on the Peoples' Democratic Party, or HDP, for alleged links to outlawed Kurdish militants. The HDP regained some of those posts but not all.