Pakistan's Supreme Court has ruled that 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.
As Mr Khan arrived in court, media ran through the corridors to capture his first public appearance since he was arrested.
Surrounded by security, Mr Khan said nothing as he walked to the wood-panelled courtroom which was filled with officials from his party and journalists.
Mr Khan stood surrounded by his lawyers in front of the three Supreme Court judges as they told him that because of the way he had been arrested on Tuesday - inside a court complex, conducting biometric tests - the arrest was invalid.
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.
"Your arrest was invalid so the whole process needs to be backtracked," Chief Justice Umar Ata Bandial told Mr Khan. He would now be under the protection of the Supreme Court.
It was then Mr Khan's turn to speak, defiant and indignant at the way he had been arrested.
The former cricketer told the judges he'd been kidnapped from the High Court on Tuesday and "hit with sticks". He was reminded several times by the judges that others had experienced worse treatment.
There was no immediate response from the security forces to the allegation.
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, which 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.
Conviction would disqualify the former international cricket star - prime minister from 2018-2022 - from standing for office, possibly for life. Elections are due later this year.
Former Pakistani ambassador Maleeha Lodhi told the BBC she thought the court's decision would help to defuse the anger of Mr Khan's supporters.
"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."
Although Mr Khan asked repeatedly to be allowed to stay at his home, the court determined that because of the security situation he would have to remain at the police guesthouse. However the judges repeatedly emphasised that he would be allowed to have whoever he chooses as a guest.
When the proceedings finished, Mr Khan sat within the court for 15 minutes taking questions from the media. He said he had not known that people had been killed during the protests or that senior members of his party had been arrested.
He told the BBC that when he was arrested he had been hit on the head and was bleeding and that when those images had circulated around the world the reaction by his supporters was not surprising.
When asked whether he would now ask his supporters to stop violent protests he said that he had already made his statement, saying he had always called for protests to be peaceful.
Supporters of Mr Khan welcomed the court's decision. "Imran Khan's release proves we knew the truth," one man, who had spent the past few days gathered outside the PTI leader's residence in Lahore, told the BBC.
Another said the verdict had "revived our hope in the nation".
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.
Here’s What Happens If Your Cheque Bounces In Qatar
Apology after Philippines tourism video uses foreign footage
Apple Is Now Worth More Than $3 Trillion, Hitting New Market Cap Milestone
Man burns Holy Quran outside mosque during Eid al-Adha