Detained former Pakistan prime minister Imran Khan was due to appear in court on Friday, after the Supreme Court ruled unlawful his arrest this week that triggered deadly clashes across the country.
"Your arrest was invalid, so the whole process needs to be backtracked," Chief Justice Umar Ata Bandial told Khan at a hearing in the capital Islamabad on Thursday.
Khan was ordered to remain in the bench's custody under police protection for his own safety until Friday's court appearance.
However, the government has vowed to re-arrest Khan should he be released, setting the stage for more unrest.
Since being ousted from office last April, Khan has waged a tempestuous campaign for snap elections and fired unprecedented criticism at Pakistan's leaders and powerful military elite.
He has accused them of plotting a November assassination attempt that saw him shot in the leg.
Meanwhile, he has become tangled in a slew of legal cases -- a frequent hazard for opposition figures in Pakistan, where rights groups say courts are used to quash dissent.
The onetime cricket star was arrested on Tuesday when, surrounded by dozens of paramilitary troops, he was taken into custody at the Islamabad High Court on corruption charges.
Arrests should not take place on court premises, Bandial said Thursday.
Khan, 70, was ordered back to the same police headquarters where he has been sequestered for the past 48 hours on the condition it should be treated as a "residence".
But interior minister Rana Sanaullah said authorities would re-arrest him.
"If he gets bail from the High Court tomorrow, we will wait for the cancellation of bail and arrest him again," Sanaullah told Dunya TV.
- Tear gas and water cannon -
Small groups of supporters danced on the streets at news the arrest had been overturned. In Lahore, police fired tear gas canisters into a crowd cheering the decision.
But with dozens of cases against him, Khan "has a long way to go", analyst Imtiaz Gul told AFP.
"This is just a timely relief, probably as part of efforts to de-escalate the explosive situation and reduce tensions," he said.
"The cobweb of criminal cases seems meant to entangle and thus incapacitate him from active politics" ahead of elections due in October, he said.
Several thousand of Khan's supporters had rampaged through cities around the country this week in protest at his arrest, setting fire to buildings and blocking roads.
At least nine people have died in the unrest, police and hospitals said.
Hundreds of police officers have been injured and more than 2,000 people arrested, mostly in Punjab and Khyber Pakhtunkhwa provinces, according to authorities.
At least eight officials from Khan's Pakistan Tehreek-e-Insaf party who were accused of orchestrating the protests were also detained, Islamabad police said.
Security forces have responded with tear gas and water cannon to quell the crowds and on Thursday came equipped with batons and riot shields.
Khan was ousted in April 2022 in a no-confidence vote in parliament after he lost the support of Pakistan's powerful military. He now alleges they are colluding with the government to keep him out of power.
Pakistani politicians have frequently been arrested and jailed since the country's founding in 1947.
But few have so directly challenged a military that holds significant influence over domestic politics and foreign policy and has staged at least three coups and ruled for more than three decades.
bur-kma/cwl