Turkish prosecutors had asked a court to jail Istanbul Mayor Ekrem Imamoglu and four of his aides pending trial on corruption charges.
Imamoglu's office revealed the request as thousands of people across the nation protested what they called his undemocratic detention.
Imamoglu, a key opposition figure and potential challenger to President Tayyip Erdogan, was detained on Wednesday for charges such as graft and aiding a terrorist group.
He has denied the charges, calling them "unimaginable accusations and slanders".
The court ruled on Imamoglu's detention early on Sunday, broadcaster AHaber said.
On Saturday, thousands of people gathered outside the Istanbul municipality building and the main courthouse, with hundreds of police stationed at both locations using tear gas and pepper spray pellets to disperse protesters as the crowd hurled firecrackers and other objects at them.
Protesters also clashed with police in the western coastal province of Izmir and the capital Ankara for a third night in a row, with police firing at the crowds with water cannons.
Imamoglu's Republican People's Party (CHP), the main opposition, has condemned the detention as politically motivated and has urged supporters to demonstrate lawfully.
The government denies any influence over the matter and says the judiciary is independent.
Imamoglu, 54, who leads Erdogan in some opinion polls, was due to be named the CHP's official presidential candidate within days.
The next election is set for 2028, but Erdogan has reached his two-term limit as president after having earlier served as prime minister.
If he wishes to run again he must call an early election or change the constitution.
On Saturday, the president, who has run the country for more than 22 years, accused the CHP of trying to "provoke our nation", adding they would not tolerate vandalism.