She also noted that since the time of the trial, no new evidence had emerged to support the prosecution, citing the lack of ballistics or serology testing, or a fingerprint match to identify Mr. Hargrave. “The scant evidence that convicted the defendant makes the newfound wrongdoing of Detective Scarcella significant,” Justice Simpson said.

Image Louis Scarcella Credit Anthony lanzilote for The New York Times

Since the trial, she also said, “potentially exculpatory evidence” had been destroyed, further undermining the chances that Mr. Hargrave could find justice.

After the judge read her decision, Mr. Hargrave’s mother broke down in tears as others around her burst out with shouts of joy.

“Thank you, your honor!” his sister Monique Hargrave shouted. “Thank you, God.” Outside the courthouse, Mr. Hargrave’s mother, Shirley, was still trying to process the fact that her son would finally be set free.

“I have never been so happy in 23 years,” she said. “I’m just so glad it’s over, and I hope it never happens to anyone else.” During the proceeding, Mr. Hargrave sat stoically, despite the celebrations behind him. His lawyer, Pierre Sussman, embraced him in a long hug.

“This is the strongest condemnation from the court of Detective Scarcella and Detective Chmil,” Mr. Sussman said. “Mr. Hargrave went in at age 17, and he’s being released at age 40.”

Justice Simpson said that the district attorney’s office had 30 days to appeal the ruling but that if prosecutors did not present new evidence in that time, any new trial would have to rely on the flawed evidence gathered by the detectives. Mark Hale, the chief of the conviction review unit for the district attorney’s office, said prosecutors were reviewing the decision before deciding how to proceed.

The decision caps a long battle to win Mr. Hargrave’s freedom. The New York Times investigated Mr. Hargrave’s case as part of a series of articles examining Mr. Scarcella’s record.