Knicks forward Julius Randle, center, celebrates with teammates after he...

Knicks forward Julius Randle, center, celebrates with teammates after he scored during final seconds of the second half of an NBA game against the Heat on Friday in Miami. Credit: Wilfredo Lee

MIAMI — The play was in shambles.

Jimmy Butler, one of the NBA’s elite defenders, was hounding Julius Randle, and twice Randle nearly lost the ball. But with no timeouts left and the clock ticking down to the final seconds, Randle had no choice but to recover and find a way to release a shot — a fadeaway three-point attempt that had him falling into the stands at Miami-Dade Arena.

And it swished.

That gave the Knicks a 122-120 win over Miami on Friday night in the latest test — heat check? — of just what they are putting together this season.

The Knicks may have acted afterward as if it were a given that the shot was going to go in, but at the time, it set off a celebration that had Randle running over coach Tom Thibodeau. He went sprawling to the court but recovered to plot out a defensive stop for the final 1.1 seconds.

“He’s a little mad at me right now,” Randle joked. “I need to talk to him. But hopefully he has a good night.”

Randle’s teammates joked about it, some pointing out that the coach would be sore in the morning. Immanuel Quickley shouted out to Thibodeau, “I was the first one to help you up!”

The Knicks (38-27) have won eight straight and 10 of 11. They have topped last season’s win total with 17 games to play.

“Fate. I don’t know,” said Randle, who shot 8-for-13 from three-point range. “We’ve been playing well. We’ve had to win many different ways this season.”

It was a celebration, maybe even a coronation, as the Knicks took a 17-point lead late in the second quarter and seemed bound for another in a line of impressive victories. And then reality came in a throwback to the battles the Knicks and Heat have waged for decades now.

Randle scored 20 of his 43 points in the first quarter and the Knicks took a 71-56 halftime lead.

After briefly going ahead midway through the fourth quarter, Miami tied it at 116 on Butler’s layup with 1:04 left.

Randle hit a fallaway jumper over Bam Adebayo, was fouled and converted the three-point play with 56.1 seconds left to give the Knicks a 119-116 lead. Tyler Herro then hit two free throws, stole the ball from Randle and drove for a layup to put the Heat up 120-119 with 23.1 seconds left.

Jalen Brunson was hounded and made a poor pass to Randle, who fumbled it but got it back near midcourt. Pressured by Butler as he drove right, he had the ball poked away but recovered it, took a dribble to his right and launched an off-balance shot to put the Knicks ahead.

The Heat had one final chance, but Mitchell Robinson (who, by the way, has made his last 18 field-goal attempts) stole Kevin Love’s inbounds pass.

“I trust 30, so any shot he takes I pretty much think is going in, especially the way he’s playing right now,” Quickley said. “He’s playing at a very, very high level. So any shot he takes I pretty much think is going in.”

“He just went crazy today,” RJ Barrett said. “When you’re in a mode like that and you’re just making shots like that, the basket just gets bigger. We needed it, for sure. He was big in just a lot of moments during the game.”

So why did Barrett think that final shot was going in?  “He had 40 already!” he said incredulously.

Brunson had 25 points and eight assists and Quickley added 21 points off the bench for the Knicks, who moved within a game of the fourth-place Cavaliers and are 4 1⁄2 games ahead of the seventh-place Heat. Butler had 33 points, shooting 18-for-20 from the free-throw line, and Herro had 29 for the Heat (33-31).

A reality check for the Knicks came in the form of injury scares. In the third quarter, Robinson swatted a drive by Butler but came down grasping his right knee and limping. He stayed in, but shortly afterward, Brunson went down after defending Butler’s fast-break dunk attempt. He almost immediately headed to the locker room, had his ankle taped, returned to the game early in the fourth quarter and picked up where he left off.

Bruised and battered, the Knicks survived — which is just how matchups with the Heat have gone through the years. Win or lose, survival is the goal.

“The one thing about the Heat is I’ve competed against them for a long time and I have the ultimate respect for them,” Thibodeau said. “They never change. The roster can change, their fight never changes. How they play, the intensity, that never changes. The hallmark is their defense and they’re going to play unselfish offensively.”

“We have a special group and it’s about taking it day by day, step by step,” Randle said. “It was a lot of energy. Friday night in Miami. Sunshine and all that stuff. A lot of Knicks fans in the building. So it was back and forth for sure.”

SUBSCRIBE

Unlimited Digital AccessOnly 25¢for 6 months

ACT NOWSALE ENDS SOON | CANCEL ANYTIME