U.S. District Judge Emmet G. Sullivan must immediately dismiss the criminal case against President Trump’s former national security adviser Michael Flynn and cannot scrutinize the Justice Department’s decision to drop the long-running prosecution, a federal appeals court ruled Wednesday.

In a 2-to-1 decision, the court said it is not within the judge’s power to prolong a prosecution or examine the government’s motives in the politically charged case. Flynn twice pleaded guilty to lying to federal agents about his pre-inauguration contacts with Russia’s ambassador before the Justice Department moved in May to dismiss the charges.

“This is not the unusual case where a more searching inquiry is justified,” wrote Judge Neomi Rao, a recent nominee of the president, in a decision that can be reviewed by the full U.S. Court of Appeals for the D.C. Circuit.

In a victory for Flynn and the Trump administration, the panel majority said Sullivan overstepped his role and committed a “clear legal error” by refusing to immediately close the case and instead appointing a former judge to argue against the Justice Department’s position.

The judge’s “demonstrated intent to scrutinize the reasoning and motives of the Department of Justice constitute irreparable harms that cannot be remedied on appeal,” Rao wrote in the 19-page opinion that was joined by Judge Karen LeCraft Henderson, a nominee of President George H.W. Bush.

Soon after the opinion was published, Trump tweeted: “Great! Appeals Court Upholds Justice Departments Request To Drop Criminal Case Against General Michael Flynn!” He later told reporters in the Oval Office: “I’m very happy about General Flynn. He was treated horribly.”

Flynn later called into Rush Limbaugh’s radio show to praise the decision, calling it a “great boost of confidence for the American people and our justice system.”

In his dissent, the third judge on the panel, Robert L. Wilkins, said the majority had “grievously” overstepped its authority by taking the “unprecedented” step to intervene midstream and force Sullivan’s hand before he had an opportunity to rule.

Sullivan “must be given a reasonable opportunity to consider and hold a hearing on the Government’s request to ensure that it is not clearly contrary to the public interest,” wrote Wilkins, a nominee of President Barack Obama.

The ruling from the three-judge panel means that at least for now Sullivan cannot hold a hearing set for July 16 to formally consider the government’s request to dismiss Flynn’s case. Sullivan could ask for a rehearing, or the full court could decide to revisit the decision from the three-judge panel, whose members are selected at random.

Attorney Beth Wilkinson, who represents Sullivan, said Wednesday, “We have no comment at this time.”

In May, Sullivan refused to sign off on the Justice Department’s plans and instead appointed John Gleeson, a former federal judge and mob prosecutor, to help him decide how to proceed. Gleeson argued that the government’s move was “highly irregular conduct to benefit a political ally of the President.”

Nothing about the case testing the powers of the judiciary to check the executive branch has followed a typical path. Flynn’s attorneys took the rare step of asking the appeals court to step in and order Sullivan to close the case.

Sullivan then hired Wilkinson, a high-profile trial lawyer, to represent him. At oral argument this month, Wilkinson told the court it was premature to cut off Sullivan’s review.

The ruling Wednesday was surprising because at least two of the judges at oral argument seemed to agree and repeatedly expressed concern that the judge would be a “rubber stamp” for the government.

Federal criminal rules require prosecutors to obtain permission from the presiding judge to drop charges against a defendant, but legal experts and former judges disagree about the limits of that judicial authority.

“Courts have said he’s not a ‘mere rubber stamp,’ ” Henderson said at oral argument of Sullivan’s independent role. “There’s no authority I know of that says he can’t hold a hearing.”

Wilkins cited past cases in which the Supreme Court upheld the power of judges “to perform an independent evaluation” of the government’s action. In his dissent Wednesday, Wilkins said the majority had eviscerated the court’s role by preventing any review.

“Today the majority declares that nevertheless — in spite of the Government’s abrupt reversal on the facts and the law, and although the Government declares itself entitled not to be forthcoming with the District Court — these circumstances merit no further examination to determine whether there may be additional reasons for the prosecutor’s actions, and if so, if any such reasons are impermissible.”

Attorneys for Flynn had accused Sullivan of bias and asked the appeals court to reassign the case to a different judge, a request the court denied Wednesday.

In a sign of the high-level interest in the matter, which could reach the Supreme Court, the Justice Department was represented by Deputy Solicitor General Jeffrey B. Wall. He referred to Gleeson’s report as a “polemic” and had urged the court not to get pulled into a “political spectacle.”

Flynn was the highest-ranking Trump adviser charged in special counsel Robert S. Mueller III’s investigation of Russian interference in the 2016 presidential election. Before its reversal, the government had recommended a prison term of up to six months for Flynn.

Before it moved to sentencing, Attorney General William P. Barr ordered a review of Flynn’s case. The review found that the FBI agents who questioned Flynn had no valid investigative basis to do so and that, therefore, any lies he told were irrelevant to any crime.

The president and Flynn’s defenders have long argued that the retired three-star general was a victim of FBI overreach. But the government’s decision to undo a guilty plea prompted criticism that the department was bending to political pressure.

The court ruling came the same day of a House hearing in which a former member of Mueller’s team testified that Barr and top deputies pressured prosecutors to give Trump’s friend Roger Stone “a break” by requesting a lighter prison sentence.

Elizabeth Wydra, president of the Constitutional Accountability Center, which filed a brief backing Sullivan, said allowing the court’s ruling to stand would erase a vital check on the executive branch.

Federal rules, she said in a statement, give the judge “a say in whether Attorney General Barr should be allowed to simply drop the matter, an act that would aid one of President Trump’s stalwart allies by essentially giving him a ‘get out of jail free’ card.”

In reviewing the government’s actions, Sullivan had also asked Gleeson to consider whether Flynn may have committed perjury while pleading guilty to a crime that he and the Justice Department now say is no longer a crime. Gleeson advised Sullivan in his initial report not to impose contempt-of-court penalties on Flynn but to continue to the sentencing phase of the case.

The court majority took issue with the appointment of Gleeson in part because of his public advocacy for an adversarial process and his initial filing with Sullivan.

“These actions foretell not only that the scrutiny will continue but that it may intensify,” Rao wrote. “Among other things, the government may be required to justify its charging decisions, not only in this case, but also in the past or pending cases cited in Gleeson’s brief.”

When new evidence surfaces, she continued, “the Executive Branch must have the authority to decide that further prosecution is not in the interest of justice.”

Spencer S. Hsu and Felicia Sonmez contributed to this report.