Advocates for detainees and human rights groups said the ruling was an important development in the legal battle over Guantánamo. They said it could reshape what have been frequent legal conflicts over administration plans to send detainees to countries where they say they face torture or mistreatment.

“It is the only time a court has said the government does not have the unfettered right to do what they will with these people,” said one of Mr. Rahman’s lawyers, Joshua W. Denbeaux.

The ruling was the latest illustration of the hurdles the government faces in its effort to reduce the number of detainees at Guantánamo. State Department officials have said their efforts to repatriate many of the remaining 330 Guantánamo detainees have been hampered by resistance from some countries and by the government’s own concerns about human rights issues.

In other cases, lawyers for detainees have tried to block transfers based on human rights concerns but have failed. Detainees’ lawyers said yesterday that they knew of no other case in which a judge had barred a transfer. Some lawyers said yesterday that they expected an appeal and that it was far from clear how appeals judges would view the ruling. Erik Ablin, a Justice Department spokesman, said the department had argued that the judge lacked the power to issue the injunction. The government, he said, is “reviewing the district court order and considering its options.”

Cynthia Smith, a Defense Department spokeswoman, said officials worked to ensure that mistreatment of transferred detainees did not occur and investigated accusations of mistreatment. “Detainees are not repatriated to countries where it is more likely than not that they will be tortured,” Ms. Smith said.