Former Pakistan prime minister Imran Khan was freed on bail by the Islamabad High Court on Friday, after his arrest on corruption charges this week sparked deadly clashes before being declared illegal.
The court also granted bail to Khan in other pending cases and ruled that he could not be arrested again before Monday.
Several thousand of his supporters have rampaged through cities across the country in protest at Khan's detention since Tuesday, setting fire to buildings, blocking roads and clashing with police outside military installations.
"The head of the country's largest party was abducted, kidnapped from the high court, and in front of the entire nation," Khan told AFP from the court building.
"They treated me like a terrorist, this had to have a reaction," he said of the protests that followed.
He left the court premises around 10.30pm – hours after the hearings ended – to travel to the city of Lahore, some 380km from the capital, his Pakistan Tehreek-e-Insaf party said.
Islamabad Police had earlier tweeted that it was responsible for Khan's security and was clearing roads, as officers used tear gas to hold back protesters who gathered a few kilometres from the heavily guard court.
Gun shots were fired at officers after dark, the force added, without giving further details.
Khan has become tangled in a slew of legal allegations – a frequent hazard for opposition figures in Pakistan – since he was ousted from power in April last year.
He had launched an unprecedented campaign of defiance against the military, which independent analysts say helped him rise and fall from power.
General elections are due no later than October, and the former cricket star has accused the shaky 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 manhandled into custody by paramilitary troops at the Islamabad High Court on Tuesday, but the Supreme Court later declared the arrest unlawful and demanded the process be "backtracked".
Re-arrest looms?
He returned to Islamabad High Court on Friday where he was granted two weeks interim bail in the graft case as well as in all other cases against him, lawyers for Khan said.
"Khan, your devotees are countless," lawyers for his party gathered in front of the court chanted as the ousted leader raised a fist above his head.
The interior minister has vowed to re-arrest Khan, who remains wildly popular.
At least nine people died in the unrest triggered by his arrest, police and hospitals said.
Hundreds of police officers were injured and more than 4,000 people detained, mostly in Punjab and Khyber Pakhtunkhwa provinces, according to authorities.
On Thursday, Chief Justice Umar Ata Bandial said Khan's 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 said.
Khan had remained in the bench's custody under police protection for his own safety until he arrived on Friday at the Islamabad High Court, where hundreds of security forces were deployed and nearby roads shut.
Internet phone services and access to social media platforms including Facebook and YouTube, which were cut shortly after Khan's arrest on Tuesday, were gradually restored around the country late Friday.
'Country needs peace'
Islamabad police had issued an emergency order banning all gatherings in the capital city after Khan's Tehreek-e-Insaf (PTI) party called for supporters to come together.
Faisal Hussain Chaudhry, a lawyer for Khan, told reporters that further arrests of senior PTI leaders had 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.
Khan's arrest 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 influence over domestic politics and foreign policy and has staged at least three coups and ruled for more than three decades.