Jason Richardson added 17 points, Shaquille O'Neal and Matt Barnes each had 16 and Leandro Barbosa scored 14 for the Suns.
Anthony Parker led Toronto with 26 points, Joey Graham scored a career-high 22 and Chris Bosh had 20 points and 11 rebounds as the Raptors lost their fifth straight.
A Stoudemire dunk with 3:18 left made it 114-109, but a putback dunk by Jamario Moon and Parker's layup drew Toronto within a point.
Parker missed a 3-pointer from the top with 37 seconds remaining and Nash converted a driving layup at the other end.
Both Parker and Bosh missed layups on Toronto's final possession and Stoudemire was fouled as he wrapped up the rebound with half a second left.
Heat 104, Thunder 94
Dwyane Wade had 32 points and 10 assists to help the Miami end an extended road trip on a positive note.
Wade went on a scoring flurry early and then came back in to stave off a fourth-quarter rally by the Thunder. He capped the night by methodically taking apart Oklahoma City's defense with a series of passes to his teammates.
Yakhouba Diawara scored 14 points after starting in place of the injured Shawn Marion, and Jamaal Magloire and Mario Chalmers each added 13 points for Miami. Udonis Haslem had 11 points and 15 rebounds.
The Heat finished 4-3 on their 12-day road trip and will host Boston on Wednesday night in their first home game since Jan. 5.
Kevin Durant led Oklahoma City with 31 points, Jeff Green scored 22 and Russell Westbrook added 14 points and eight rebounds.