U.S. District Judge Richard J. Leon said Thursday that the five men could not be held indefinitely as enemy combatants.
The judge said the military can continue to detain a sixth Algerian — Belkacem Bensayah — who was captured with the other five.
One of the men to be released is Lakhdar Boumediene, whose landmark Supreme Court case last summer gave the Guantanamo detainees the right to challenge their imprisonment.
Their lawyers say there's no evidence the men ever would have ended up on a battlefield or posed any threat to the U.S.
The government initially detained Boumediene and the other five men on suspicion of plotting to bomb the U.S. Embassy in Sarajevo in October 2001. They were transferred to Guantanamo in January 2002.
The Justice Department has since backed off embassy bombing accusations, but maintain the six men were caught and detained before they could join terrorists' global jihad against the United States and its allies.
The Bush administration says it was right to be proactive against threats like those allegedly posed by the Algerians, especially in the months immediately following the terror attacks on New York and Washington on Sept. 11, 2001.