The worst season imaginable set the stage for a moment too good to believe.

More than six weeks removed from its most recent win, St. John’s penned an all-time program moment, and ended an 11-game losing streak, by stunning No. 4 Duke, 81-77, at a sold-out Madison Square Garden on Saturday afternoon.

After Grayson Allen’s game-tying 3-point attempt missed in the final seconds, the only Big East team winless in conference play had knocked off a national championship contender, with the Red Storm (11-13, 0-11) picking up their first win since Dec. 20.

The agony had finally stopped, swapped for a memory that will outlast any other from this miserable season.

“We would’ve took it anywhere. We would’ve taken a win if it was in an empty gym,” coach Chris Mullin said. “To turn it around against one of the storied programs in college basketball, it’s great, and probably more important for our players to get that taste back in their mouth.

“I’m happy for the guys. They needed it as bad as anything.”

With St. John’s facing an opponent with five soon-to-be first-round draft picks, Shamorie Ponds was the best player on the court, taking the torch from Marcus Hatten and Dwight Hardy, and lighting up Duke (19-4, 7-3), with 33 points, seven rebounds and four steals. St. John’s last defeated Duke in 2011.

Though the Red Storm blew an 11-point advantage with less than seven minutes remaining, Ponds wrestled back the lead on a drive with 1:19 remaining, and put St. John’s up four, on a 3-pointer with the shot clock winding down, and 40 seconds left.

“It was probably the biggest play of the game, no question,” Mullin said. “He had it going. He had a nice feel. I thought he was moving really well, backing guys off and creating space. He mixed up being an aggressive scorer and being a playmaker.”

Because St. John’s entered with a string of close losses — including to No. 1 Villanova, and No. 6 Xavier — Duke coach Mike Krzyzewski insisted his team didn’t look past their struggling opponent, but the Blue Devils were lethargic, and extremely careless (18 turnovers), coasting on skill and size to a 39-32 halftime lead.

Ponds kick-started a 10-2 run to open the second half, and electrified the red sections of the split crowd after putting St. John’s up for the first time with 17:01 remaining.

“We hit them in the mouth, and knocked them off-balance, and we kept our foot down,” said Tariq Owens, who had 17 points. “Our team has never lost confidence. … We never felt like it was a lack of talent.”

Krzyzewski, with a team that has more talent than any in the country, grew incensed, stripping off his jacket, and standing from his chair, watching Marvin Bagley III, a potential top-three pick, commit six turnovers, and Allen shoot 1-of-7.

Despite timely shooting from Bashir Ahmed (19 points), the Blue Devils recovered, and took a 73-72 lead on two Gary Trent Jr. (22 points) free throws with 1:35 remaining, but Duke missed three free throws in the final 1:08.

Security gathered, blocking the sidelines to prevent a potential court storming. An NYPD officer clapped throughout the final seconds, as St. John’s players smiled wider than they remembered was possible.

A win had finally come — that will live longer than any victory of Mullin’s three seasons.

“The team that was deserving of winning won.” Krzyzewski said. “I thought they made us look bad, but then we made ourselves look bad. … We were like five individuals out there, and it was disgusting, really.

“We got what we deserved. … We weren’t able to knock them out after we lost the first 12 rounds of a 15-round fight.”

Injured guard Marcus LoVett was in attendance, but didn’t sit on the bench with the team.

Sporting St. John’s gear, the sophomore said he was “not sure” if he would return to play for the Red Storm next season.