ATLANTA — The Knicks were in desperate need for a score in the closing seconds after their 16-point fourth-quarter lead nearly evaporated, and they got it - but not in the way you would expect.

Carmelo Anthony, Kristaps Porzingis and Arron Afflalo were on the floor and having big games. But they weren’t involved in the play that sealed the game for the Knicks. The Hawks took away the Knicks first two options, so it was Robin Lopez feeding Jose Calderon cutting to the basket.

Calderon’s layup put the Knicks up four with 18 seconds to go, and they held off the Hawks, 107-101, Tuesday night at Philips Arena.

The Knicks improved to 17-19, matching their win total from all of last season. It was also their third win in four games, and they swept the home-and-home with the Hawks.

“It’s a good win for our group more so than a particular number,” Derek Fisher said. “The players themselves are starting to feel like something is building.”

Clinging to a 103-101 lead after one Dennis Schroder free throw with 40.3 seconds left, the Knicks’ first option was screen-roll with Anthony and Afflalo in the corner was the second. Neither was open.

advertisement | advertise on newsday

Calderon thought about shooting but when Lopez flashed to the lane, he gave him the ball near the elbow. When a second defender converged, Calderon cut and Lopez saw him and fed him for the layup.

“I saw the whole backdoor, like the whole basket open,” Calderon said. “He gave me a great pass and I finished. For the team, It felt great. I’m always happy to be able to help in that situation.”

The Knicks, who continue this three-game trip in Miami tonight, still needed a stop on the next trip. They got it when Paul Millsap dribbled the ball off his foot and out of bounds. Anthony was fouled on the inbounds pass and made two free throws with 13.4 seconds left to ice the game.

“It’s hard to beat a team twice in a row, home and home,” Anthony said. “I thought this team showed some grit, some will. We just had a different focus coming into tonight’s game.”

Anthony finished with 23 points, 11 rebounds and seven assists. Afflalo, who torched the Hawks for 38 points Sunday, also scored 23. He shot 9-of-16 overall and 2-for-2 from three-point range, and was 23-33 (9-for-11) in the two games. Porzingis had 17 points and 11 rebounds.

Knicks videos

Porzingis picked up his first technical foul of the season after Kent Bazemore got up in his chest and the two were jawing following a foul late in the game. Anthony was the first one to get in the middle and defend Porzingis.

He was fouled after grabbing an offensive rebound and sank both free throws with 1:51 left to put the Knicks up 101-95.

“The game at the end was really tight and that stuff happens,” Porzingis said.

Millsap had 19 and Jeff Teague 16 for the Hawks (21-15).

The Knicks led 87-71 after Derrick Williams opened the fourth with a three-pointer, but their defense started to bend and the Hawks took advantage. They trimmed 10 points off the deficit and cut it to 95-89 following a wide-open three by guard Teague.

advertisement | advertise on newsday

Afflalo answered on the other end to put the Knicks up eight. The lead didn’t get under six until Millsap’s two foul shots with 2:16 to go made it 99-95.

This time Afflalo missed inside, but the Knicks got two offensive rebounds. After Porzingis made two from the line to put the Knicks up six, Horford connected on a three-pointer six seconds later to bring Atlanta within three.

Anthony gave the Knicks a 103-98 lead with a pull-up jumper with 1:22 remaining. Although their defense bent, it never broke and they locked up somewhat-meaningful win 17.

“I try not to remember anything about last season,” Anthony said. “This is a new beginning for us, a new start for us. I’ll take these 17 wins and as much as we want to celebrate the 17 wins we got a chance to go get 18 tomorrow.”