Dwyane Wade added 19 points for the Heat, who shot 47.6 percent from 3-point range (10 of 21) and 51 percent from the field overall.
Chandler Parsons scored a career-high 25 points and Omer Asik had a career-best 19 points and 14 rebounds for the Rockets, who led 110-106 with 90 seconds left.
James went 3 for 4 from the free throw line over the next minute, then drove for the go-ahead layup with 19 seconds left.
Jeremy Lin shot an airball from 3-point range with 10 seconds left, Wade caught it and the Heat escaped with their sixth straight win over Houston.
The Heat, coming off a 104-86 loss in Memphis on Sunday, looked as though they might blow out Houston early when they opened the game with a 20-5 spurt. Miami started 8 for 12 from the field, while the Rockets missed nine of their first 11 shots.
Wade won the individual matchup with James Harden early on, too, holding him scoreless through the first 8 minutes. Harden got his first field goal in four attempts on a short baseline jumper over Mario Chalmers, but that only cut Miami's lead to 22-9.
Harden scored 10 points with Wade on the bench and his straight-on 3-pointer just before the quarter buzzer cut the deficit to 25-19. Wade was back in for the start of the second quarter and immediately turned a steal into a breakaway layup.
Miami led 56-52 at halftime, as its shooting cooled and Houston found scoring opportunities inside. James had just six points on 2-for-6 shooting and missed his only 3-point attempt in the first half.
James sank two 3s early in the third quarter to get himself going. But Lin hit a 3-pointer and then threaded a nifty bounce pass to Parsons for a fast-break layup, as the Rockets rallied again. Carlos Delfino's 3-pointer from the wing with 5:03 left in the third quarter gave Houston its first lead at 70-69.
The Rockets continued to find holes in the middle of the Heat's defense to keep Miami at bay. James scored 16 points in the third quarter, but rimmed out a 3-pointer just before the buzzer and Houston led 85-82. Houston had 36 points in the paint and 14 second-chance points after three quarters.
Marcus Morris sank two unlikely 3-pointers from the corner over James, and Houston seemed to be on its way to an upset. Parsons' 3 from the corner with 1:29 remaining put Houston up 110-106.
But the Rockets couldn't score again, and Harden missed a 3 at the buzzer with James guarding him closely.
Harden shook off the slow start to score 22 points. Lin had only nine points and six assists.
Notes: James scored in double figures for the 149th consecutive game for Miami, breaking a tie with Wade for the longest streak in franchise history. ... The Rockets were still uncertain when coach Kevin McHale would rejoin the team. McHale missed his second straight game to attend to a family matter. ''It's a tough time for his family,'' interim coach Kelvin Sampson said. ''Our thoughts and prayers are certainly with him.'' ... Houston Texans general manager Rick Smith and star receiver Andre Johnson, a Miami native, had courtside seats next to prominent defense attorney Rusty Hardin, a regular at Rockets games. Houston running back Arian Foster sat behind them, and James ran over and greeted both players just before the opening tip. ... Shane Battier, who played for the Rockets from 2006-11, got a loud ovation when he was introduced in Miami's starting lineup. The Rockets played a tribute video after the first quarter and Battier acknowledged more applause with a wave.