A notorious Brooklyn Hasidic pedophile nearly walked free during a resentencing Tuesday — until he pretended to forget details of his crimes and the furious prosecutor reversed course and pushed for more time.

Nechemya Weberman ended up with a sentence slashed down from 103 years to 18 years, although with time served, that amounts to just five years left on the clock for the pedo — and with good behavior, he could be out by 2028.

“Mr. Weberman’s outrageous behavior in court today personified why he remains a danger to society,” said Sarena Townsend, the lawyer for the sicko’s then-12-year-old female victim.

“His repeated denials and refusal to face his actions proved that his assurances of rehabilitation and remorse were lies.”

Weberman, a former counselor at a Williamsburg yeshiva now in his 60s, appeared vivacious, smiling and jovial on video feed from the maximum-security Shawangunk Prison in upstate New York — despite his supporters painting an image of a feeble man at death’s door.

He had been convicted on 59 counts and originally sentenced to 103 years in prison 13 years ago for repeatedly sexually abusing his victim for three years.

The Brooklyn District Attorney’s Office — under pressure from some members of the convict’s religious community for years — was prepared to effectively push to free the convicted sicko, having sought the resentencing on the grounds his 103-year stint was excessive and that he had already served enough time.

But Brooklyn Assistant District Attorney Joseph Alexis changed course mid-hearing when Weberman claimed to forget the gory details of his misdeeds.

The pedophile attested to his remorse and apologized, reading from a statement, claiming he had spent years “in denial’’ and was “a changed man.’’