The Supreme Court in Islamabad ruled on Thursday that the arrest of former prime minister Imran Khan was illegal.
The three-judge bench had been asked to hear Khan's petition against his arrest on Tuesday, on charges of corruption.
Why has Khan been released?
"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, ordering the 70-year-old's release from custody.
Khan immediately asked his supporters to end violent protests across the country in which at least eight people were killed and nearly 300 wounded, according to the Interior Ministry.
What happens next?
Khan's lawyer, Babar Awan, emphasized to reporters that his client is a "free citizen" having been released from the custody of the National Accountability Bureau (NAB), which brought the original charges against him. But he will nevertheless remain in the custody of the court.
He will spend Thursday night in the same police residence as before but with permission to receive visits from up to ten friends and family members.
On Friday, he will once again appear before the high court to seek a formal bail, and he has been urged to cooperate with the authorities.
Analyst Imtiaz Gul told the AFP news agency that Khan still "has a long way to go," explaining:
"This is just a timely relief, probably as part of efforts to de-escalate the explosive situation and reduce tensions."
Gul suggested that "the cobweb of criminal cases seems meant to entangle and thus incapacitate him from active politics" ahead of elections due in October.
Government interior minister Rana Sanaullah told Dunya TV: "If he gets bail from the High Court tomorrow, we will wait for the cancellation of bail and arrest him again."
How have Khan's supporters reacted?
Khan's supporters could be seen dancing near the court building to celebrate his release, but further clashes soon erupted.
Police swung batons and fired tear gas, while protesters with sticks smashed police car windows.
In other Pakistani cities, local media reported life slowly began to return to normal and streets were mostly quiet on Thursday except for sporadic protests.
Why was Khan arrested in the first place?
After he was forced from power in a no-confidence vote by lawmakers, Khan has faced multiple corruption charges in Pakistani courts.
The former international cricket star was indicted on claims that he unlawfully sold state gifts while serving as prime minister from 2018 to 2022. His arrest was based on a new warrant for a separate corruption case related to property.
Khan was being held at a police compound in Islamabad after a judge ordered he be detained for at least eight more days, raising the prospect of further unrest.
At least two provinces asked Pakistan's federal government to deploy troops to restore order amid a surge of violence following Khan's arrest.
Pakistan's government insisted that the Pakistan Tehreek-e-Insaf (PTI) party had planned the unrest, with Khan's supporters attacking important state buildings and damaging private and public vehicles.
Khan told the court on Thursday that he had been unaware of what was happening across the country since his arrest. He told the court he had been "treated like a terrorist."
What are the allegations about gifts?
The "Toshakhana" gift-selling case is one of several legal battles that the former international cricket star and his populist center-right party are facing.
It hinges on a government department known as the Toshakhana, which refers to Mughal-era treasure houses kept by royal rulers to store and display gifts bestowed on them.
Although government officials have to declare all gifts, they are allowed to keep those below a certain value and, in some cases, they can buy back more expensive presents at a discount.
Khan and his wife received lavish gifts worth millions during foreign trips, including luxury watches, jewelry, designer handbags, and perfumes. The former prime minister is alleged to have failed to declare some of the presents, or the profit made from selling them.
The Election Commission of Pakistan has already barred Khan from holding public office until the next election because of the claims that he sold the presents. Successive governments in Pakistan have previously targeted political opponents by filing legal cases against them to keep them away from politics. Khan and his supporters claim the proceedings are politically motivated.
mfo/rc/jcg (AFP, Reuters, dpa)