A court in Cairo dropped its case Saturday against former president Hosni Mubarak on charges that he ordered police to kill hundreds of protesters during the 2011 uprising, in a ruling that rights activists said demonstrates the impunity ­enjoyed by ex-regime figures years after the pro-democracy revolt.

The court also acquitted the former strongman’s top security aides of the same accusations.

The presiding judge said prosecutors had waited too long to file their indictment against Mubarak, canceling the court’s jurisdiction over the case. More than 800 people died during the 18-day revolt that ousted Mubarak nearly four years ago, rights groups say.

The verdict drew hundreds of people to the outskirts of Tahrir Square in protest Saturday night. The army and police had sealed off the square — the epicenter of the 2011 rebellion — to prevent any anti-government demonstrations. About 9 p.m., security forces moved to disperse the peaceful protesters who had been chanting against the military and the government, using tear gas, water cannons and birdshot to scatter the demonstrators, witnesses said.

Earlier in the day, the courtroom crowd at the police academy in Cairo erupted in applause after the ruling was read. The judge also cleared Mubarak and his two sons of separate charges of corruption, including a case in which Mubarak was accused of conspiring with longtime confidant and businessman Hussein Salem to sell gas to Israel at below-market prices.

Pro-Mubarak crowds, cheering and playing nationalist songs, gathered Saturday afternoon outside the Cairo-area military hospital where Mubarak is staying.

“It’s a good thing that he was declared innocent. This is proof Egypt will never fail,” Mohamed Ibrahim, a 36-year-old carpenter, said outside the hospital in the Cairo suburb of Maadi. “This is a slap in the face to the Americans. They sold him out.”

The former president has been under house arrest at the hospital while serving a three-year sentence handed down in May for the misuse of public funds.

Egyptian legal experts disagreed Saturday over whether Mubarak would be allowed to walk free or would still serve time at the hospital for the previous conviction. According to Egypt’s criminal code, if a defendant spends time in pre-trial detention but is later acquitted, that time counts toward any other prison sentences the defendant must serve. Mubarak was jailed in May 2011 on a number of charges, which would mean he has completed the three-year sentence for embezzlement.

Mubarak, however, was not acquitted Saturday: The case was dismissed on procedural grounds. There are no other charges pending against him.

Several hours after the verdict, Mubarak told the private television channel Sada el-Balad in a telephone interview from the hospital that he was innocent.

“I never did anything wrong, so I just waited for what the court would present, and I was declared innocent,” Mubarak said.

Egyptian anti-Mubarak protesters gather near Tahrir Square in Cairo after hearing the verdict of former Egyptian president Hosni Mubarak Saturday. (Islam Farouk/EPA)

But rights lawyers here criticized the court’s handling of the case. In 2012, the ex-strongman and his onetime interior minister, Habib al-Adli, were sentenced to life in prison for their role in the protesters’ deaths. They were later granted a retrial.

The indictment covered only six days of the police response to the rebellion that began in January 2011 when demonstrators rose up against Mubarak’s 30-year-old regime. Prosecutors investigated Mubarak and his aides for the deaths of 239 people in 10 provinces.

Egyptian security forces were notorious for their brutality under the Mubarak government. But lawyers here say the prosecution struggled to make its case.

“There was no real evidence [against Mubarak], so this was an inevitable outcome,” said Mohamed Zaree, a lawyer at the Cairo Institute for Human Rights Studies. He added, “There was never any real intention to try Mubarak.”

Heba Habib contributed to this report.