Antti Niemi stopped 34-of-37 shots for the win.
Jamie Benn posted a goal and two assists for the Stars, losers in three of four.
Benn drew the Stars to 4-3 at the 8:11 mark of the third, when he converted a Steve Ott pass with a backhander on the doorstep.
The home team failed to equalize despite two power plays in the final four- plus minutes. Heatley drew the first minor, for interference with 4:06 to go, but the Sharks kept the Stars at bay.
After Logan Couture was sent to the box for delay of game with 2:01 remaining, Thornton hit the empty net to make it 5-3 less than a minute later.
Despite continued pressure in the San Jose zone, Pavelski gained control of a loose puck and dished ahead to Heatley, who flung it the length of the ice for another score with the Dallas net vacant.
Dallas jumped out to a 2-0 lead on scores by Ribeiro and Petersen in a 1:40 span early in the second. Peterson's tally was a short-handed score, a redirection of a Benn dish into the slot.
San Jose knotted the score thanks to Thornton at 6:23 and Heatley on a power play at 12:40.
The visitors then forged ahead with goals less than five minutes apart early in the third period. Clowe made it 3-2 at 1:51 on a wrister from in close, then Mitchell struck at 3:31 from the right side due to a Pavelski set-up.
San Jose improved to 13-6-2 in Dallas since 2005...Stars forward Loui Eriksson left the game with an upper-body injury in the second period after absorbing a hit from Sharks defenseman Douglas Murray, and did not return...The Sharks avenged an 8-2 setback to the Stars in Dallas almost one year ago.