This mess was over, pure and simple. When Tim Hardaway Jr. dunked with 5:17 left in the game Friday night, the Knicks were up 17 on the lowly 76ers.

On the bench, Carmelo Anthony removed his headband. Tyson Chandler figured he was done for the night. Amar’e Stoudemire stayed warm on the absurd chance he would be needed. But J.R. Smith’s thought was “Oh, no.”

That’s because on the floor, the Knicks were going through five misses, five turnovers and a 1-of-2 from the line. The Sixers? They were compiling a 16-1 run, capped by a Thaddeus Young 3-pointer that made it a two-point game with 32.9 seconds remaining. If coach Mike Woodson still had hair, he would have yanked it out.

So back into the game went the starters and as Chandler noted, “a win is a win” so none are going to forfeit the 93-92 blowout-turned-nail biter that became the Knicks’ eighth straight victory and a ludicrous 23rd consecutive defeat for the Sixers.

“Never been,” Anthony, who scored 21 points and had eight rebounds, said of being in that call-back situation before. “First time. First time for everything.”

The Knicks averted what could have been a devastating defeat because Atlanta, the team they are trying to catch for the eighth playoff spot, lost. So the Knicks are just three games behind with 13 to play.

“We still have to dominate. We got a ways to go. With them losing one game, we’re still three games out. And so we got to make up the difference, we got to still keep playing,” said Stoudemire, who had 22 points, 10 rebounds for his third double-double of the season.

There seemed to be every reason to relax. The gosh-awful Sixers, who lost their 18th straight at home, should have packed it in — but they didn’t. Instead of checking the train schedule down 17 with just over 5:00 on the clock, they rallied. Then rallied some more. And more. The Knicks had to warm up — quick.

“We didn’t have no choice at that point,” Anthony said about the fast readiness factor. “I felt like we had a very comfortable lead. It happened. … They were just a group of guys that didn’t quit and played to the end.”

After Young made it 92-90, Raymond Felton, Anthony, Smith and Stoudemire reported back. After a Hardaway miss was rebounded by Sixer Michael Carter-Williams (22 points, 13 rebounds, nine assists), Philadelphia called time. On came Chandler (17 points, 10 rebounds) and Iman Shumpert. Philly caught what was plaguing the Knicks and turned over the ball. More subs with variations.

“It’s happened. A number of times it’s happened in my coaching career,” Woodson said about putting starters back in the game. “I’ve got to trust my younger players too and this is the first time I’ve had this group where we’ve held the lead, put them in three minutes on the clock and they can’t hold it. I’ve got to be careful the next time coming down the home stretch.”

After the Sixers’ mistake, they fouled Anthony, who made 1-of-2 at the line with 7.3 seconds left for a 93-90 Knick lead. That left it up to Carter-Williams whose 3-point attempt went in and out. A putback by Young (21 points) provided the final score.

“It was just one of those games where they had a streak and we hadn’t been playing well pretty much the whole game,” Smith said. “It was one of those things you think anything can happen. Fortunately we came out with a win.”