Former prime minister Imran Khan’s dramatic arrest on corruption charges this week was illegal.
The court ordered Mr Khan’s immediate release. His lawyers had argued that his detention from court premises in Islamabad on Tuesday was unlawful.
At least 10 people have been killed and 2,000 arrested as violent protests have swept the country since he was held.
Tuesday’s arrest escalated growing tensions between him and the military.
The opposition leader, ousted in a confidence vote in April last year, was brought to court on the orders of Pakistan’s top judge.
“Your arrest was invalid so the whole process needs to be backtracked,” Chief Justice Umar Ata Bandial told Mr Khan.
The former cricketer told the judges he’d been kidnapped from the High Court on Tuesday and “hit with sticks”. There was no immediate response from the security forces to the allegation.
Footage of his arrest showed paramilitary forces seizing Mr Khan, who was injured in a gun attack last year, and dragging him from inside court premises, before whisking him away in an armoured vehicle.
His Pakistan Tehreek-e-Insaf (PTI) party says the cases against him are politically motivated. The arrest enraged his supporters – the past 48 hours have seen widespread violence and rare attacks on state and military facilities.
Seven senior PTI leaders are among those arrested. They include former foreign minister Shah Mahmood Qureshi, who police say “incited violence”. In a statement Mr Qureshi denied this and urged supporters to continue with peaceful protests.
Mr Khan has been kept at a police guesthouse in the capital since Tuesday. It remained unclear when he would be allowed to leave – the Supreme Court turned down his request to move to his own accommodation saying he should remain where he is for his own safety.
The guesthouse was turned into a makeshift court on Wednesday where a judge formally charged him with corruption for the first time in the dozens of cases he faces. He pleaded not guilty.
The last couple of days have seen extraordinary violence by protesters who are supporters of Imran Khan, who are obviously enraged by the manner in which he was arrested,” she said.
“There is still uncertainty, but hopefully the situation will not return to the kind of violent scenes that we have seen.”
Many analysts believe Mr Khan’s election win in 2018 happened with the help of the military, which both parties denied. But he later fell out with the powerful army. After a series of defections, and amid mounting economic crisis, he lost his majority in parliament.
Since being ousted less than four years into his term, he has become one of the military’s most vocal critics, and analysts say the army’s popularity has fallen.
In November 2022, Mr Khan was shot in the leg while leading a protest march. He has accused a senior intelligence officials of carrying out the attack – which the military has strongly denied.
A day before his arrest, the military warned him against making “baseless allegations” after he again accused a senior officer of plotting to kill him.
Observers see the country facing an unprecedented series of crises – given endless political turmoil, the economy on a cliff edge and mounting violence by Islamist militants eroding confidence in the security forces.