By Jonathan Stempel

NEW YORK, Jan 18 (Reuters) - The U.S. Department of Defense must release a cache of photos showing how Army personnel treated detainees at the Abu Ghraib prison and other sites in Iraq and Afghanistan, a federal judge ruled on Wednesday.

U.S. District Judge Alvin Hellerstein in Manhattan said the release was proper because departing Defense Secretary Ash Carter failed to show why publishing the photos would endanger Americans deployed outside the United States.

Hellerstein's decision is a victory for the American Civil Liberties Union and other civil and veterans rights groups whose lawsuit seeking the photos under the federal Freedom of Information Act began in 2004.

Photos depicting abuse at Abu Ghraib began to emerge in 2004, with some detainees claiming to have endured physical and sexual abuse, electric shocks and mock executions.

The number of photos sought in the lawsuit has not been disclosed but has been estimated at roughly 2,000, according to the Congressional Record and court papers.

"Those photos, representing a sad episode in our history, are a matter of great public interest and historical importance, which should not, in a democracy like ours, be shielded from public view," said Lawrence Lustberg, a lawyer for the plaintiffs. "The court has wisely reaffirmed our nation's commitment to open government."

A spokesman for U.S. Attorney Preet Bharara in Manhattan, whose office defended Carter's decision, declined to comment.

After Hellerstein in March 2015 ordered the release of additional photos, Carter allowed the release of 198 but kept the remainder under wraps, citing a review of a "representative sample" by four high-ranking generals.

In Wednesday's decision, Hellerstein said the U.S. troop presence in Iraq had fallen to about 5,000 from more than 100,000 at the start of the Obama administration, and that those remaining now serve as advisers rather than in combat.

The judge said that while risks remained, including that portions of Iraq had been "overrun" by the Islamic State, he could not blindly accept withholding the remaining photos.

"I take seriously the level of deference owed to the executive branch in the realm of national security decision making," he wrote. "My complaint is that the executive has failed to articulate the reasons supporting its conclusion that release of the photos would endanger Americans deployed abroad."

Hellerstein first ordered the release of photos in 2005, but Congress later authorized withholding photos whose release could endanger Americans.

The case is American Civil Liberties Union et al v. Department of Defense et al, U.S. District Court, Southern District of New York, No. 04-04151.

(Reporting by Jonathan Stempel in New York; Editing by Toni Reinhold) ((jon.stempel@thomsonreuters.com; +1 646 223 6317; Reuters Messaging: jon.stempel.thomsonreuters.com@reuters.net))