A federal judge in Washington on Saturday blocked President Trump from ousting the leader of a federal watchdog agency, saying that the effort to remove the official without due cause had violated the law.

In an order on Saturday evening, Judge Amy Berman Jackson granted a permanent injunction against the government, allowing Hampton Dellinger to remain the head of the Office of Special Counsel, which protects federal whistle-blowers.

The order required the Trump administration to recognize Mr. Dellinger’s authority in that position, barring it from taking any action to “treat him in any way as if he has been removed” or otherwise interfere with his work.

The administration immediately moved to challenge the ruling, starting an appeals process that appeared likely to end at the Supreme Court.

In a 67-page opinion explaining the order, Judge Jackson, of the U.S. District Court for the District of Columbia, stressed the unique responsibilities Congress gave the office when it was created under a 1978 law. She noted its central role in protecting whistle-blowers in the federal government, a role that she said would be compromised if Mr. Dellinger were allowed to be removed without a cause stipulated under the law.

“It is his independence that qualifies him to watch over the time-tested structure that is supposed to bar executive officials from taking federal jobs from qualified individuals and handing them out to political allies — a system that Congress found intolerable over a century ago,” she wrote. “The position would be entirely ineffective if the special counsel were to be compelled to operate with the sword of at-will removal hanging over his head.”

Mr. Dellinger was confirmed by the Senate for the role in 2024 for a five-year term.

But on Feb. 7, he received a memo from the White House notifying him that he was fired, without any explanation. Several days later, Judge Jackson issued a temporary order allowing Mr. Dellinger to stay in place while litigation continued.

During a hearing on Wednesday, lawyers representing the government argued that Mr. Dellinger’s role was comparable to that of other heads of federal agencies who are appointed by the president. They said that the office Mr. Dellinger runs has significant investigatory powers, arguing that as president, Mr. Trump should be able to ensure the office is run by a person sharing his agenda.

Mr. Dellinger’s lawyers described the job as limited in scope, with only the authority to start inquiries and no power to enforce subpoenas. But they insisted that the role, as envisioned by Congress, should come with independence and some legal protections.

Earlier this week, Mr. Dellinger said the Office of Special Counsel was investigating the president’s move to fire thousands of probationary workers. The federal Merit Systems Protection Board said that it would reinstate six workers while the watchdog agency continued to investigate.

Judge Jackson’s ruling shielding Mr. Dellinger came a week after the Supreme Court declined to lift the temporary block on his removal. Lawyers for the government argued to the court that Mr. Trump had expansive executive authority to place his preferred pick in charge of the office.

While the justices’ order declining to intervene was unsigned, some on the court suggested that they might return to the matter.

Justices Sonia Sotomayor and Ketanji Brown Jackson said that they would have rejected the Trump administration’s request for Supreme Court intervention outright. Justice Neil M. Gorsuch, joined by Justice Samuel A. Alito Jr., filed a dissent, noting that it “may not yet have ripened into an appealable order” in the eyes of the majority but that the case could soon make its way back up to the court.

A future challenge before the court could provide an early test of the justices’ appetite to restrain Mr. Trump’s executive power, but Judge Jackson’s order made clear her belief that the Office of Special Counsel should be insulated from politics.

She said that without a more substantive reason related to his performance, Mr. Dellinger could not be fired “on a whim or out of personal animus.”

“The Special Counsel’s job is to look into and expose unethical or unlawful practices directed at federal civil servants,” she wrote, “and to help ensure that whistle-blowers who disclose fraud, waste, and abuse on the part of government agencies can do so without suffering reprisals.”

“It would be ironic, to say the least,” she added, if the “special counsel himself could be chilled in his work by fear of arbitrary or partisan removal.”

Adam Liptak contributed reporting.