The two-time champion rebounded from fighting to remain on the lead lap early on and then from an unscheduled pitstop for a suspected puncture to be in position to contend for victory following a two-tyre pitstop during a late caution.
Restarting from third after his final stop, where most of the fastest drivers of the day went for a full new set of Goodyears, Stewart was able to move up to second but following another caution he would drop to third behind Richard Childress Racing's Jeff Burton.
A further restart allowed him to pass Burton again, but ahead Johnson was able to pull a decent gap that would have been hard to overcome without the 18th and final caution of the day, in a race that turned into a demolition derby for some as drivers lost patience in the tight confines of the half-mile track.
Stewart restarted from the outside of the front row when the green waved for the final time, immediately attacking Johnson for the lead. Although it is unusual to be able to pass on the outside at Martinsville, the Stewart Haas racer found grip where others had not, clearing Johnson with two laps to go to claim his third win in this year's Chase.
The victory vaults Stewart up to second in the play-off standings, only eight points off Roush Fenway's Carl Edwards.
"I don't think anybody has ever passed Jimmie Johnson on the outside so, [it was] just determination, I don't think we had the best racecar today by any means," said Stewart about his pass for victory. "Carl Edwards had better be real worried. That's all I've got to say. He's not going to sleep for the next three weeks."
Johnson was disappointed to lose at arguably his best track, the final caution probably costing him the win. He was critical of Brian Vickers for repeatedly causing incidents, the final one being costly for him as the Red Bull driver spun to bring out the caution that allowed Stewart a final go at him.
During one of the late cautions where most of the leaders pitted for tyres, Johnson stayed out in the lead and although his crew chief Chad Knaus thought he had thrown away his chances of winning, Johnson believes they still were in position to score another Martinsville victory.
"Chad made a great call," said Johnson, who moved up one place in the Chase but is still 43 points back. "I thought it was going to bite us, staying out like we did; and with all the cautions that gave us an opportunity to win the race.
"And Tony hung on tough on the outside there and I just couldn't bring myself to leaning on him and moving him out of the way with where he is in the points. I just kind of raced him clean and then he got around on the outside and got in front of us."
Johnson's team-mate Jeff Gordon led for 114 laps after recovering from being involved in the first incident of the day, ignited by contact between an unusually aggressive Dale Earnhardt Jr and Penske's Kurt Busch. Richard Childress Racing's Kevin Harvick finished a solid fourth, which moved him up two spots in the Chase, followed by Joe Gibbs Racing's Denny Hamlin, one of 12 different race leaders.
Edwards had a tough day in which he simply struggled for balance and speed for most of the running, twice recovering from being a lap down thanks to the free-pass rule. However he managed to stay out of trouble in a day when it proved hard to do so and eventually finished ninth, the highest he ran all day after dropping in the order from pole early on.
"It's unreal," said Edwards. "We were so bad. With about 200 laps to go I had become okay with the fact that we were probably going to finish 20th or 25th. I was thinking already about Texas and how we were going to have to go there and everything we were going to do, but my guys stuck with it and we got very, very fortunate. I'm just glad we can move on."
The Roush driver avoided a black-flag penalty for seemingly gaining positions on a restart before crossing the start-finish line. However officials rescinded the penalty as Edwards was apparently recovering his correct spot for the restart at the last minute.
His team-mate Matt Kenseth got into an exchange with Vickers but then with 36 laps to go he got into Kyle Busch, igniting a multi-car crash where both were badly damaged, sending Kenseth to the garage for lengthy repairs.
The Penske duo of Brad Keselowski and Kurt Busch were also involved in incidents, the former spinning in the closing laps due to contact while running in the top 10.
Results - 500 laps: Pos Driver Team/Car Time/Gap 1. Tony Stewart Stewart Haas Chevrolet 3h49m52.000s 2. Jimmie Johnson Hendrick Chevrolet + 0.170s 3. Jeff Gordon Hendrick Chevrolet + 0.444s 4. Kevin Harvick Childress Chevrolet + 1.014s 5. Denny Hamlin Gibbs Toyota + 2.080s 6. Jeff Burton Childress Chevrolet + 2.214s 7. Dale Earnhardt Jr Hendrick Chevrolet + 3.079s 8. Martin Truex Jr Waltrip Toyota + 3.300s 9. Carl Edwards Roush Fenway Ford + 3.599s 10. Ryan Newman Stewart Haas Chevrolet + 3.733s 11. AJ Allmendinger Petty Ford + 4.299s 12. Casey Mears Germain Toyota + 4.647s 13. Regan Smith Furniture Row Chevrolet + 4.975s 14. Kurt Busch Penske Dodge + 5.096s 15. Greg Biffle Roush Fenway Ford + 5.846s 16. Travis Kvapil Front Row Ford + 7.810s 17. Brad Keselowski Penske Dodge + 14.932s 18. Joey Logano Gibbs Toyota + 17.204s 19. Clint Bowyer Childress Chevrolet + 23.120s 20. David Reutimann Waltrip Toyota + 23.126s 21. Ken Schrader FAS Lane Ford + 1 lap 22. Juan Pablo Montoya Earnhardt Ganassi Chevrolet + 3 laps 23. Dave Blaney Baldwin Chevrolet + 3 laps 24. Paul Menard Childress Chevrolet + 3 laps 25. Kasey Kahne Red Bull Toyota + 5 laps 26. Hermie Sadler TRG Ford + 7 laps 27. Kyle Busch Gibbs Toyota + 7 laps 28. Mark Martin Hendrick Chevrolet + 9 laps 29. Marcos Ambrose Petty Ford + 12 laps 30. Brian Vickers Red Bull Toyota + 16 laps 31. Matt Kenseth Roush Fenway Ford + 23 laps 32. Bobby Labonte JTG Daugherty Toyota + 36 laps 33. David Ragan Roush Fenway Ford + 56 laps Retirements: David Gilliland Front Row Ford 357 laps Jamie McMurray Earnhardt Ganassi Chevrolet 85 laps Reed Sorenson Gordon Dodge 77 laps Scott Speed Whitney Ford 74 laps David Stremme Inception Chevrolet 52 laps Michael McDowell HP Toyota 45 laps JJ Yeley Front Row Ford 40 laps Joe Nemechek NEMCO Toyota 33 laps Landon Cassill Phoenix Chevrolet 27 laps Mike Skinner Max Q Ford 7 laps