A federal judge on Friday ordered the Trump administration to restore the press credentials of Jim Acosta of CNN, handing the cable network an early win in its lawsuit against the president and members of his administration.

Presiding over one of the first major tests of press rights under President Trump, Judge Timothy J. Kelly of United States District Court in Washington ruled that the White House had behaved inappropriately in stripping Mr. Acosta of his press badge shortly after a testy exchange at a news conference last week.

The administration’s process for barring the correspondent “is still so shrouded in mystery that the government could not tell me” who made the decision, Judge Kelly said from the bench. Taking away the pass that gave Mr. Acosta access to the White House amounted to a violation of his right to a fair and transparent process, the judge ruled.

Soon after the ruling, Mr. Trump said the White House would tighten its rules for how journalists must comport themselves at the White House.