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. It was Robin Lopez feeding Jose Calderon on a give-and-go.

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

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

After Calderon’s basket, Paul Millsap dribbled the ball off his foot and out-of-bounds. Anthony was fouled on the inbounds and made two free throws with 13.4 seconds left to ice the 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-for-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.

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 Jeff Teague.

Afflalo answered on the other end to put the Knicks up eight. The lead didn’t get under six until Paul 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. Porzingis was fouled going up with his board and hit both free throws to make it 101-95 with 1:51 to go. 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. After Dennis Schroder’s layup, Porzingis couldn’t convert on the Knicks end. Schroder was fouled but hit just one free throw that made it 103-101 with 40.3 seconds left.

Afflalo missed more shots in the first half (five) than he did the entire game Sunday. Duplicating that torrid pace would have been hard to accomplish. Afflalo shot 14-for-17 two days ago.

But the Knicks had some balanced scoring in the first half and led 42-32 after an Afflalo layup with 4:31 left in the second quarter. They were up 54-49 at the break and ahead for most of the half. Atlanta held a lead for just 21 seconds.

Anthony, who scored 13 in the first half, had three assists by halftime. The third quarter opened with Anthony feeding Afflalo on back-to-back possessions for three-pointers. He assisted on three of the Knicks’ first four baskets, helping them extend to 64-51.

The overall mix of selfless, team-oriented players has Anthony being a distributor and facilitator more than ever in his Knicks’ career because he’s developed a trust with them.

“If everybody on this team was out for self it would make it difficult not just for me but for everybody to trust each other,” Anthony said. “Guys on this team are not out for self. So I can rely on them to if they have it they can make a play for themselves and if they don’t they can make a play for somebody else.”

Anthony was asked if this was one of his favorite groups of his career.

“It’s starting to become that,” he said. “I had a couple of teams where we had different personalities but we had a good time. We had some good times. I had some teams were personalities didn’t all match up. That’s what happens in this league.”

The Knicks opened the third with a 19-8 run and took a 73-57 lead after Robin Lopez’s three-point play with 6:54 left in the quarter. The Hawks got it under 10 twice in the third, but a Porzingis’ three-point play with 21.3 seconds left sent the Knicks into the fourth with an 84-71 lead.