Former Pakistan Prime Minister Imran Khan has been granted bail by the Islamabad High Court after his arrest on corruption charges this week sparked deadly clashes before being declared illegal.

“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 on Friday.

The high court also ruled that Khan, who remained on the court premises, could not be arrested before Monday in any other case registered against him, including charges related to the violent riots ignited by his detention this week, said another one of his lawyers, Tahir Malik.

Khan has become entangled in a slew of legal allegations since he was removed from power in April last year by a no-confidence vote in parliament and then launched a defiant campaign against the military.

General elections are due no later than October, and the former cricket star has accused the shaky incumbent coalition government of ousting him in cahoots with top generals.

The 70-year-old has also made explosive claims that they masterminded a November assassination attempt, which saw him shot in the leg as he campaigned for snap polls.

The interior minister has promised to rearrest Khan, who remains wildly popular, and police fired tear gas on protesters who marched towards the high court.

Khan was arrested 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”.

On Friday, Khan returned to court in a convoy and walked into the building flanked by dozens of police and paramilitaries.

“Khan, your devotees are countless,” lawyers for his party chanted in front of the court as he raised a fist above his head.

Speaking to reporters inside the courtroom after being granted bail, Khan blamed the army’s commander, General Syed Asim Munir, for the situation in the country.

“It’s not the security institution; it is just one man, the army chief,” Khan said. “There is no democracy in the army. The army is getting maligned by what is happening right now in the country.”

“That one man is afraid that when I come to power, I will remove him from his positions, but I will do no such thing,” the opposition leader said.

Labyrinthine legal cases

Khan’s arrest under the orders of Pakistan’s top anti-corruption agency earlier this week triggered two days of chaos, 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 4,000 people arrested, mostly in Punjab and Khyber Pakhtunkhwa provinces, according to authorities.

The government contended that Khan’s release rewards and encourages mob violence.

Khan was arrested in what is known as the Al-Qadir Trust case. It concerns land that Khan and his wife Bushra Bibi bought from property tycoon Malik Riaz for their Al-Qadir University Trust . The anti-corruption agency, the National Accountability Bureau, has alleged that Khan’s government struck a deal with Riaz in a quid pro quo arrangement in which it is accused of helping Riaz launder more than $239m while causing a loss to the national exchequer.

On Thursday, Chief Justice Umar Ata Bandial said Khan’s arrest in the case was illegal 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 closed.