Toronto allowed a 15-point lead to dwindle to four at halftime but regrouped in the third quarter, getting it up to 16. The Raptors then hung on after the lead got down to two near the end of the game.

Nate Robinson nailed a 3-pointer with just under a minute left, and Carlos Boozer hit two foul shots with 16.8 seconds remaining to pull Chicago within 99-97.

DeRozan then made two free throws to get the lead up to four, but the Bulls weren’t finished. Robinson hit the first of two free throws with 10.6 free throws left and, after a jump ball following the miss, there was a scramble for the loose ball.

From the floor, Boozer threw it to Robinson. The ball went through his hands and started to go into the backcourt. The guard saved it off Toronto’s Amir Johnson to avoid the violation but missed a heave at the buzzer.

Richard Hamilton returned for Chicago after missing more than a month with a back injury, but with Joakim Noah (right foot), Luol Deng (hip), Taj Gibson (left knee) and Derrick Rose (left knee) sidelined, the Bulls struggled.

That preserved the win for the Raptors, but an injury to center Jonas Valanciunas sure cast a cloud over it.

He walked off the court clutching his neck after colliding with Gay during that scramble and was tended to for several minutes on the sideline before being wheeled off on a gurney.

For the fifth-place Bulls, the loss left them just a half-game ahead of Atlanta in the Eastern Conference with five to play. It also damaged their chances of catching Brooklyn, a team they trailed by two when the night began, for the fourth seed and homecourt advantage in the first round of the playoffs.

Gay shook off a poor performance after matching a season low with four points in Saturday’s loss to Milwaukee.

DeRozan came up big. Terrence Ross came off the bench to score 13. Kyle Lowry added 13 points and 10 assists, and the Raptors shot over 51 percent.

Hamilton was a surprise addition to the rotation after coach Tom Thibodeau said at the shootaround that he would not play. Instead, he got in 15 minutes and finished with four points after missing the previous 19 games, but Chicago again stumbled badly after falling to Detroit on Sunday.

The Raptors broke it open midway through the third, scoring 12 straight to turn a four-point lead into a 76-60 advantage.

Gay had seven points during that run, capping it with a 3-pointer with 3:26 left in the quarter, but things got interesting at the end.