Judge Leon, who was appointed by President Bush, ruled in 2005 that the men had no habeas corpus rights, and he had been expected to be sympathetic to the government in the current case.

As he read his decision in a quiet courtroom, he seemed to bridle at the Supreme Court’s ruling, saying its effect was “to superimpose the habeas corpus process into the world of intelligence gathering.”

He said his decision, which involved men first detained in Bosnia far from the war in Afghanistan, should not be read as a reflection on the strength of the cases against other detainees, more than 200 of whom have filed habeas corpus cases. “This is a unique case,” he said.

Still, there was a buzz in the gallery when he announced that the government had not proved its case against the five men. In urging the government not to continue to fight the case, he noted that an appeal could take as long as two years.

“Seven years of waiting for our legal system to give them an answer to a question so important is, in my judgment, more than plenty,” he said.

The men listened to the proceedings over a phone line from Guantánamo. If the government appeals they could remain there indefinitely. If it does not, one of their lawyers, Stephen H. Oleskey, said he expected that they would return to Bosnia, where they would be freed.

In a statement, the Justice Department did not say whether it would appeal, only that it was reviewing the case. It added that the ruling was “perhaps an understandable consequence of the fact that neither the Supreme Court nor Congress has provided rules on how these habeas corpus cases should proceed in this unprecedented context.”