The win tied the best-of-seven Western Conference opening series at 3-3 going into game seven on Saturday in Dallas.
The Boston Celtics also avoided elimination with a 92-89 win over the Indiana Pacers in Indianapolis as Antoine Walker scored 24 points, including the clinching basket in overtime.
The Eastern Conference first round series is also tied at 3-3 ahead of Saturday's seventh game in Boston
In Houston, the Rockets went into the fourth quarter down by a point, but outscored Dallas 32-13 to take the win.
The rally was keyed by Jon Barry who scored 10 points in just over two minutes midway through the period to open a 10-point lead Houston never relinquished.
"I had a lot of help tonight," McGrady said. "I had a lot of guys that stepped up big for me and we played with a sense of urgency.
"It was win or go home and we definitely didn't want to go home right now, so we came out and played big."
Barry finished the game with 14 points, while Mike James had 22 for the Rockets. The 7-6 center Yao Ming had just eight points and five rebounds.
Jerry Stackhouse scored 21 and Dirk Nowitzki 19 to lead the Mavs.
The Rockets won the first two games against Dallas before losing three in a row to teeter on the edge of elimination.
Four games were decided by four points or less in a tough series that has included a record $100,000 fine for Rockets coach Jeff Van Gundy, who accused the league of telling referees to watch Yao closely for fouls, as suggested by Dallas owner Mark Cuban.
"We didn't play well enough to win the game. We don't make excuses. We lost the game. The game was called fairly, pretty much, and that's the way it is. No excuses, no complaining, no nothing."
Houston coach Jeff Van Gundy is just happy to be heading for a seventh game.
"We'll let her rip on Saturday and see what happens," he said.
The Celtics overcame the ejection of Paul Pierce late in regulation time to force a seventh game with the Pacers.
Boston trailed early by as many as 11 points but the Pacers lost the lead and were then behind for most of the rest of the game until the dying seconds.
With the Celtics leading 84-83, Pierce was intentionally fouled by Jamaal Tinsley and reacted by swinging at his opponent who hit the floor.
Referee Steve Javie huddled with the other officials and assessed Pierce his second technical foul of the game, an automatic ejection.
Reggie Miller then hit the technical foul shot to tie the game but Boston's Kendrick Perkins missed both attempts for the Celtics to tie the game.
Miller's final shot in regulation went wide, setting the stage for overtime.
Ricky Davis had 22 points, five rebounds and four assists for the Celtics, while Pierce had 20 points, 11 rebounds and six assists.
Jermaine O'Neal had 26 points and 10 rebounds for the Pacers, who won game five at Boston but missed the chance to win the series at home.
Stephen Jackson had 14 points and five rebounds for the Pacers.