The order by U.S. District Judge Gladys Kessler gives the government 15 days to comply and allows only two exceptions: if the detainee is a material witness to a terror investigation and if the person being held does not want to be identified.
Kessler rejected the Justice Department's argument that releasing names would allow terrorist groups to gauge the progress of the government's investigation.
In her opinion, Kessler wrote that in the government affidavits "nowhere declare that some or all of the detainees have connections to terrorism. Nor do they provide facts that would permit the court to infer links to terrorism. For example, the government has provided no information on the standard used to arrest and detain individuals initially."
The Justice Department detained almost 1,200 people following the attacks and most already have been deported.
The government disclosed in June that at least 147 people still were being held, including 74 on charges involving immigration infractions. Prosecutors have not said how many are being held as material witnesses.
Kessler also ruled Friday that the government must release names of attorneys representing the detainees. She said lawyers, unlike suspects, have no expectation of privacy and are well equipped to "handle their own affairs."
Kessler did uphold the government's right to keep secret the locations of where detainees are being held. She said there is a significant risk that the prisons could be targeted by those angered by the detentions.
Groups suing the government to disclose the names include the People for the American Way Foundation, the Center for National Security Studies, the American Civil Liberties Union and the Electronic Privacy Information Center.
The groups argued that, by keeping the names secret, the government expanded its own power without any congressional support and in conflict with statutes on public information.
"This secrecy, though now linked to Sept. 11, would allow the secret jailing of any immigration violator for any reason, as long as they are linked to a pending investigation," plaintiffs' attorney Kate Martin argued. "The government acknowledges that many of those still being held, in secret, are no longer suspected of being involved in a terrorist group. That is too much power."
Justice Department attorney Ann Weisman had argued primarily that releasing the names would reveal too much to terror groups like Osama bin Laden's al-Qaida network.
"Terrorists may be able to map the progress of the investigation by communicating with those detained and charting who has been detained and where," Weisman said. "Terrorist groups could decide to switch to an alternate cell if one cell is proven to be compromised by a detainment."