Pakistan's Supreme Court declared on Thursday the arrest of former prime minister Imran Khan "invalid", in a ruling that came two days after his detention sparked deadly clashes and huge protests nationwide.
Khan was ordered to remain in the custody of the court under police protection for his own safety until his appearance at another court hearing on Friday.
"Your arrest was invalid so the whole process needs to be backtracked," Chief Justice Umar Ata Bandial told Khan at the hearing in the capital Islamabad.
"What we propose is that Islamabad police need to provide security, and he (Khan) will provide a list of his immediate family members and lawyers that should meet him at police lines headquarters," said Bandial.
He added that the headquarters, where Khan was taken into custody following his arrest, should instead be treated as a "residence".
Bandial denied Khan's request to return to his farm house on the outskirts of Islamabad.
"The court has ordered that he will remain in the custody of the Supreme Court of Pakistan and that he can meet and consult his friends and lawyers during his stay there," Khan's lawyer, Babar Awan, told reporters.
Pakistan's top judge had ordered police to bring Khan before him in response to a petition by his political party challenging his arrest on Tuesday on corruption charges.
On Thursday, Khan told the apex court he had been "treated like a terrorist".
Several thousand supporters of his Pakistan Tehreek-e-Insaf (PTI) party have for two days rampaged through cities around the country, 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, police added.
At least eight officials from PTI's central leadership 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.
Supporters turning up to a protest venue in Karachi were swiftly rounded up while in Islamabad police beat handcuffed PTI supporters, AFP journalists saw.
'To civil war'
The former cricket superstar was ousted in April 2022 in a no-confidence vote in parliament after he lost the support of Pakistan's powerful military.
Khan has said the dozens of legal cases brought against him following his ousting are part of an effort by the government and the army to prevent him from returning to power ahead of elections due in the autumn.
The military earlier issued a statement warning of a "severe reaction" to any further attacks on state and military facilities, and said responsibility for any violence would lie with "a group that wants to push Pakistan into civil war".
Khan's party dismissed the statement as "contrary to facts and the situation on the ground".
Internet cut, social media blocked
Since Wednesday, the army has been deployed in two provinces – including Punjab, the most populous – and in the capital.
The interior ministry has ordered mobile internet services cut and restricted access to social media sites Twitter, Facebook and YouTube, Pakistan's communications agency said.
Authorities also closed schools nationwide – with year-end exams cancelled.
"People should protest but it doesn't mean setting fire and damaging government property, or harming poor people and damaging their cars," said 45-year-old Syed Muzaffar Shah in the city of Peshawar.
Pakistan is struggling through a severe economic downturn, brought about in part by the deepening political instability of the past year, with the rupee plummeting to a record low against the dollar on Thursday.
Red line
Khan was arrested hours after the military rebuked him for alleging that a senior officer was involved in a plot to assassinate him.
The army has denied Khan's accusation.
Criticism of Pakistan's military establishment is considered a red line.
Pakistani politicians have frequently been arrested and jailed since the country's founding in 1947.
But few have so directly challenged a military which holds significant influence over domestic politics and foreign policy and that has staged at least three coups and ruled for more than three decades.
Khan was remanded in custody Tuesday following a request by Pakistan's top anti-corruption agency, the National Accountability Bureau, one of his lawyers told AFP.
The agency had earlier accused Khan of ignoring repeated court summons over alleged corruption linked to a trust fund he set up with his wife, a spiritual healer.
Khan was also indicted without being arrested over separate accusations he mislead officials about gifts he received from foreign leaders while in power, according to the Election Commission of Pakistan.