A Brooklyn man who spent the last 24 years behind bars walked free Friday after a judge tossed out the indictment that put him there.

Brooklyn Supreme Court Justice Sharen Hudson ordered Shawn Williams’ case dismissed, and his immediate release, after prosecutors joined a defense motion asking the jurist to set aside Williams’ conviction and vacate the original indictment.

Williams’ is the 14th overturned conviction linked to retired NYPD Detective Louis Scarcella, a once-renowned officer who in recent years has faced allegations of misconduct by a number of since-freed people he put away.

“I would like to thank my lawyers for helping me achieve this great victory today,” Williams, 41, said outside court. “After 20-something years incarcerated, I finally get the opportunity to relive my life as a free man. It was a long and hard-fought battle. I was sent to prison for a crime I didn’t commit. My childhood friend was murdered, and I was framed for it.”

Williams has been held in the upstate Clinton Correctional Facility since he was convicted in 1994 for the fatal shooting of his neighbor and pal Marvin Mason in 1993.

The case against Williams began to fall apart in 2013 after defense attorney Victor Hou, of Cleary Gottlieb, and associates with the Legal Aid Society tracked down the sole eyewitness to the Crown Heights slaying.

The eyewitness, Margaret Smith, recanted her testimony, claiming she’d been coerced into naming Williams by Scarcella, who maintains his innocence.

Prosecutors Friday didn’t mention Scarcella, simply saying Smith was in “very ill” health and “uncooperative,” and that they would not retry the case.

“While Mr. Williams can never have his years of wrongful incarceration returned to him, we hope that he and his family can find some measure of peace in this vindication,” said Hou.