A man behind bars for nearly four years for a gang rape that did not happen has been cleared after his accuser admitted she lied to make her friends feel sorry for her.

William McCaffrey hugged his lawyer after the same New York judge who had sentenced him to 20 years in prison threw out the case and apologised on Thursday.

"I've been waiting for this for a long time," the softly spoken McCaffrey, 32, said outside a Manhattan court.

"I'm just glad it's over."

New DNA tests played a part, but his exoneration hinged largely on his accuser recanting - a rarity after rape convictions, the head of a national prosecutors' group said.

State Supreme Court Justice Richard Carruthers called the case "a catastrophe" for both the criminal justice system and Mr McCaffrey, who was in jail from his arrest in 2005 to his release on $5000 bail in September.

"I convey to you my personal regrets for having participated, though unknowingly, in the injustice," said Justice Carruthers, who had called the purported attack "disgusting" during Mr McCaffrey's sentencing in 2006.

Biurny Peguero, then 22, originally said three men, led by Mr McCaffrey, raped her at knifepoint after luring her into their car after a night out in 2005.

Mr McCaffrey said she had agreed to go with them to a party, and they dropped her off unharmed after she changed her mind.

Peguero told her story to a grand jury, took the stand again at Mr McCaffery's trial and said at his sentencing that the "tragedy changed my life forever".

He was convicted of charges including rape and kidnapping and received a 20-year prison term.

No one else was convicted.

Defence lawyer Glenn Garber of the Exoneration Initiative, a New York-based group that provides free legal help challenging convictions, later persuaded prosecutors to use new technology to retest DNA samples from an apparent bite mark on Peguero's arm.

The initial tests were inconclusive. The new ones showed the genetic material not only was not Mr McCaffrey's but came from at least two women, apparently friends of Peguero who fought with her.

Separately, Peguero confessed her lie to a priest and then to authorities this year. She claimed she was raped because she wanted her friends "to feel badly" for her, and then was afraid to back down from her story as the case continued, prosecutors said in court filings in autumn.

She thought Mr McCaffrey would ultimately be acquitted because of a lack of other evidence, prosecutors said.

Peguero, who now uses the name Biurny Gonzalez, pleaded guilty to perjury last Monday.

The mother of two - the younger born last month - could face up to seven years in prison when she is sentenced in February.

Her lawyer, Paul Callan, said she was "very, very happy" about Mr McCaffrey's vindication.

"My client has been working diligently over the last seven months to see that this day would come," he said.

Statistics on the prevalence of recanted rape allegations vary widely, but most unravel before anyone is convicted, said Scott Burns, the executive director of the National District Lawyers Association and the former DA of south-west Utah's Iron County.

Law enforcement officials and women's advocates have striven for decades to ensure sexual assault allegations are taken seriously, "so it's extremely damaging when this happens", Mr Burns said.