Knicks forward Julius Randle shoots past Heat center Bam Adebayo...

Knicks forward Julius Randle shoots past Heat center Bam Adebayo during the first half at Madison Square Garden on Sunday. Credit: Kathleen Malone-Van Dyke

The last time the Knicks saw Julius Randle, he was emotionally pummeled. He was being helped into a side room next to the visitors’ locker room at the Staples Center, where he got the details of the loss of his grandmother, Annie Page, last week.

He missed two games and flew back late Saturday night, joining his team in time to take the floor Sunday afternoon at Madison Square Garden. And when the Knicks took on the Heat, Randle didn’t feel alone. He had his teammates, whom he’d cried with at their own family losses this season, and he felt as if he had his grandmother with him.

That made his 26 points and key buckets down the stretch special as he helped the Knicks end their losing streak at five games, 124-121. They outscored the Heat 40-27 in the fourth quarter.

“It’s extremely tough,” Randle said. “Lot of highs, a lot of lows. My grandmother left an incredible legacy and mark on my life and my family. It’s been tough. It’ll still be tough. But tonight was special. I really felt her out there the whole time.”

The Knicks trailed by 14 points late in the third quarter, but Randle scored 11 points in the fourth quarter and gave the Knicks their first lead of the second half at 116-114 when he hit a three-pointer with 2:16 to play. He added a pair of free throws and then a layup with 34.9 seconds remaining to put the Knicks up 122-119.

Randle is the latest Knick to lose a family member this season, following Dennis Smith Jr. and Reggie Bullock. In those times, he served as a friend and consoler. His teammates did the same for him at this difficult time.

“Yeah, it’s tough,” Randle said. “To lose somebody so important to you, like you said, we had a couple incidents, a couple things have happened to us as a group. It affects you. It affects all of us. I cried when these dudes lost their family members. It’s tough. I felt the same compassion from them, losing my grandmother. It’s tough. Got to keep going.

“It was hard. It was hard. But they’ve been unbelievably supportive from top to bottom. Ownership. Steve [Mills], Scott [Perry], coaches, teammates. Everybody has been amazingly supportive.

“It makes the process easier. It’s never going to be an easy process, but it relieves a little bit of stress and pain away. Just being around these guys, like I said, this is family.”

RJ Barrett added 23 points and Kevin Knox had 17 for the Knicks (11-29). Jimmy Butler had 25 points, 10 rebounds and six assists for Miami (27-12).

For much of the night, the sounds from the crowd at the Garden were loud chants of “Let’s go Heat!” But in the final minutes, the loud cries of “De-fense!” echoed through the arena. And despite the usual rising point total, it didn’t sound as if the home crowd was mocking the Knicks.

The cries were for a stop, just one stop, to try to change the fortunes of a slumping team.

But as the fans screamed for a stop, Butler launched an off-balance three-point attempt and was fouled by Bullock with 21.9 seconds left. After the Knicks unsuccessfully challenged the call, Butler made the first two free throws to bring the Heat within 122-121 but clanked the third shot off the front of the rim.

Barrett grabbed the rebound and was fouled with 16.8 seconds to play, missing the first free throw before hitting the second for a two-point lead.

After a timeout, Butler spun into the lane and missed a layup, but the ball went out of bounds off the Knicks, giving Miami another shot with 12 seconds left. Butler drove, and as the defense converged, he tried to slip a pass to Bam Adebayo but committed a turnover. Barrett picked it up, was fouled and added one of two from the line to close out the scoring.

Miami had one last chance with five-tenths of a second remaining. Adebayo got the inbounds pass and threw up a desperation turnaround three-pointer that slipped cleanly through the net, but time had expired.