Former Pakistan prime minister Imran Khan was granted bail by the Islamabad High Court on Friday, after his arrest on corruption charges this week sparked deadly clashes before being declared illegal.
But Khan was not immediately released and instead had to answer further cases.
"The court has granted Imran Khan two weeks interim bail and has directed the authorities not to arrest him in the graft case," Khan's lawyer Khawaja Harris told reporters afterwards.
Khan was manhandled into detention by paramilitary troops at the Islamabad High Court on Tuesday, but the Pakistani Supreme Court later declared the arrest unlawful and demanded the process be "backtracked".
On Friday, he arrived in a secure convoy and walked into the building flanked by dozens of police and paramilitaries.
Lawyers for his party gathered in front of the court ahead of the hearing and shouted "Khan, your devotees are countless" and "the lawyers are alive", as the ousted leader raised a fist above his head.
The interior minister has pledged to re-arrest Khan, who has become tangled in a slew of legal cases -- a frequent hazard for opposition figures in Pakistan -- since he was ousted from power last April and launched a campaign of defiance against the military.
General elections are due no later than October and the former cricket star has accused the shaky incumbent coalition government of supplanting him in cahoots with top generals.
The 70-year-old has also made explosive claims that they puppeteered a November assassination attempt which saw him shot in the leg as he campaigned for snap polls.
Khan was arrested under the orders of Pakistan's top graft agency as he arrived for a routine court appearance in the capital.
Two days of chaos followed, with several thousand of his supporters rampaging through cities across the country in protest, setting fire to buildings and blocking roads.
At least nine people died in the unrest, police and hospitals said.
Hundreds of police officers were injured and more than 3,500 people arrested, mostly in Punjab and Khyber Pakhtunkhwa provinces, according to authorities.
On Thursday, Chief Justice Umar Ata Bandial said the arrest was unlawful because it took place on court premises, where Khan had intended to file a bail application.
"Your arrest was invalid, so the whole process needs to be backtracked," he told Khan.
Khan remained in the bench's custody overnight under police protection for his own safety until he arrived at the Islamabad High Court, where hundreds of security forces were deployed, and nearby roads shut.
Islamabad police had issued an emergency order banning all gatherings in the capital city after Khan's Tehreek-e-Insaf party called for supporters to come together.
Faisal Hussain Chaudhry, a lawyer for Khan, told reporters that further arrests of senior PTI leaders overnight brought the total number to 10.
"The country needs peace but such steps by the government are not helpful," he said.
Despite the ruling on the legality of Khan's arrest, Interior Minister Rana Sanaullah refused on Thursday to back down.
"If Khan gets bail from the High Court tomorrow, we will wait for the cancellation of bail and arrest him again," Sanaullah told Dunya TV.
“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."
Khan has remained wildly popular since being ousted.
His arrest this week came after the army rebuked him for once again repeating allegations, they were involved in his assassination attempt.
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.