The arrest of such a prominent military figure in a country that has experienced three coups has unsettled Pakistan at a time when it is preparing for historic elections on May 11.

Saturday’s order came at a hearing in an Islamabad court, where Musharraf was brought under heavy security as supporters and opponents gathered outside the court.

The judge ruled that he would be given judicial remand, which means that he would be held in custody until the next hearing on May 4. Musharraf’s legal team had been pushing to get his estate on the edge of Islamabad declared a temporary jail under Pakistani law. The Islamabad chief commissioner later issued a notification declaring the residence a jail, said police official Mohammed Khalid.

After the court hearing, Musharraf was taken back to a police guesthouse where he had spent the night, and then was transported in the late afternoon to his private house on the outskirts of the capital.

Musharraf was arrested in the case that stems from his decision, while in power, to sack and detain the judges, including the country’s chief justice, after declaring a state of emergency and suspending the constitution. At the time, Musharraf apparently was concerned that the judges would push back against his re-election as president. As a justification for the state of emergency he also cited the growing Taliban insurgency in the country’s northwest.

But the move backfired horribly.

The country’s lawyers took to the streets in widespread protests that eventually weakened Musharraf’s government so much that he was forced to call new elections and step down.

A judge has said that Musharraf’s 2007 decision amounted to terrorism, which is why the case is now being heard before an anti-terrorism court. Such courts are closed to the media and the public.

Musharraf returned to Pakistan to make a political comeback and contest the May 11 election. But he was greeted with little popular support and was disqualified from running in the election.

A judge on Thursday ordered his arrest. That sparked Musharraf’s dramatic escape from court in a speeding vehicle. After getting away, he holed up in his heavily guarded house on the outskirts of Islamabad until he was taken into custody on Friday morning.

In a message posted on his Facebook page after his arrest, Musharraf wrote: “These allegations are politically motivated, and I will fight them in the trial court, where the truth will eventually prevail.”

Musharraf seized control of Pakistan in a coup in 1999 when he was army chief and spent nearly a decade in power before being forced to step down in 2008. He returned despite Taliban death threats and a raft of legal challenges. He also faces legal charges in connection with the 2007 assassination of the former prime minister Benazir Bhutto and the 2006 death of a Baluch nationalist leader.

But it has been the case related to the judges that has sparked the most contention since Musharraf’s return — a reflection of the deep animosity many in the legal sphere still have for the former strongman.

His arrest is a significant act in a country where senior army officers have long seemed untouchable. The army is still considered the most powerful institution in Pakistan, but its aura of impunity has declined in recent years, especially in the face of an activist judiciary.

When Musharraf entered the court Saturday, he was surrounded by a phalanx of police and paramilitary Rangers.

Pakistani lawyers chanted: “Whoever is a friend of Musharraf is a traitor.” Supporters shouted: “Long live Musharraf!”

Musharraf had hoped to regain some of his power by winning seats in the May 11 election. That vote will mark a significant milestone for Pakistan which has seen its democratic evolution stalled by a history of military coups.

The winner of the presidential election will continue to face rising militancy in violence in the country.

On Saturday, a suspected suicide bomber targeted security forces outside the main gate of a hospital in Khar, the main town in the Bajur tribal region. Four people died in the blast, said political administration official Abdul Haseeb Khan. The tribal regions border Afghanistan to the west and have been the scene of numerous attacks by militants, who are opposed to the Pakistani government and want to impose their version of Islam on the rest of the country.