The win wasn't secured until Gallinari connected on a long jumper with 36.8 seconds to go for a 102-101 Knicks lead and David Lee sank a layup with 7.3 seconds left to extend New York's advantage to three.
The Celtics' Rasheed Wallace missed a long 3-point try as time expired and the Knicks won for only the second time in six games.
Lee finished with 13 points, 11 rebounds and six assists while Earl Barron chipped with 17 points and 18 rebounds for New York.
Ray Allen led the Celtics with 17 points. Boston's recent poor play continued with its fourth loss in five games. The Celtics stayed one game behind Atlanta in the race for the third seed in the Eastern Conference.