Thirteen defendants charged with or convicted of distributing large amounts of heroin in the District had their cases dismissed Friday because prosecution had been tainted by an FBI agent who is accused of tampering with evidence linked to the cases, including drugs and guns.

Seven of the defendants had pleaded guilty in the drug conspiracy and four of them were serving prison sentences of between two and seven years. But in one instant, all were unburdened by criminal charges or convictions, and those serving time had their sentences vacated.

As soon as the hearing in federal court adjourned, the former defendants rushed from the box normally reserved for jurors, with one jubilantly saying, “Let me get out of here before [the judge] changes his mind.”

Friday’s ruling in U.S. District Court follows similar action Thursday, when another judge threw out charges involving 10 people convicted in a separate drug conspiracy involving the sale of heroin and cocaine. Charges in other cases could be dropped in the near future.

Prosecutors had recommended dropping the charges amid the investigation of Matthew Lowry, 33, an FBI agent assigned to the Washington field office who worked with police on crime that spilled over from the District into Maryland and Virginia. Lowry has been suspended but not charged. The investigation is being led by the Justice Department’s inspector general, and court documents link the dismissals to the probe involving Lowry.

Authorities have said little about the investigation, but court documents filed as part of the release of suspects say that Lowery is accused of tampering with drug and gun evidence. Officials with knowledge of the investigation have said the agent allegedly took heroin and used it himself. Other officials said Lowry was found in the last week of September slumped over the wheel of an unmarked FBI car near the Navy Yard, along with two drug evidence bags, heroin and two guns.

The fallout has been swift. Within days, prosecutors identified at least three drug cases and started to send defendants home from jail and prison to await further action. Prosecutors began dismissing cases outright Thursday. In all, 23 of 28 defendants in two drug cases have had their charges dismissed.

“We do not lightly dismiss these cases, particularly when the defendants face the serious drug charges at issue here,” U.S. Attorney Ronald C. Machen Jr. said in a statement.

But, he added, “The credibility of the system is paramount and more important than any individual prosecution. That’s why we are carrying out a case-by-case, defendant-by-defendant review to determine which cases should go forward.

“We are carefully examining the role that the FBI agent played in each investigation to assess whether the case can proceed. And moving forward, be assured we will not be dissuaded from aggressively investigating and prosecuting narcotics cases to protect the residents of the District of Columbia,” the prosecutor said.

Prosecutors have refused, at least for now, to drop two other drug cases involving more than 35 defendants in which the agent reportedly had a peripheral role.

At Friday’s hearing, U.S. District Judge Reggie B. Walton told the group they had “dodged a bullet” and said he hoped they stayed out of trouble. “I don’t know if you did what you were accused of doing or not, but if you did do it, I hope I don’t see you again,” he warned.

One of the defendants freed, 59-year-old Brandon Beale, went straight from the courtroom to the Pretrial Services Agency so he could turn in his ankle bracelet and shed the restrictions of home detention, said his attorney, Greg English. English said Beale, who was jailed for nearly a year, planned to fight the charges on the grounds that he was merely a drug user, not a dealer. Now, English said, there will be no reason to do that.

“This turn of events is absolutely extraordinary for an agent to commit misconduct like this. It goes to the basic integrity of the system,” English said. “But having said that, I think the U.S. Attorney’s Office did the right thing today and dismissed it. . . . They were completely ethical and upfront in their conduct in this case.”

English said it was technically possible for prosecutors to bring the charges against Beale and others again, but he doubted they would do so.

Matt Zapotosky contributed to this report.