And then he drove away with ease on three restarts over the final 52 laps, ending the three-time defending series champion's remarkable run of five trips to Victory Lane in the previous six races at the shortest circuit in NASCAR's Sprint Cup Series.
Hamlin used an out-of-sequence pit stop after about 160 laps to move to the front, then held on up there until the entire leaderboard headed for pit road about 20 laps later.
"Once we got that track position, our car just kind of took off," he said.
The finish shaped up as another possible drama involving Hamlin and Johnson, who nudged Hamlin aside with 15 laps to go at Martinsville in the spring and sailed off to an easy victory.
But with NASCAR having implemented double-file restarts since then, it was no trouble.
"The only difference was it was double-file restarts versus single file and you actually feel a little bit better with him right beside you than behind you," Hamlin said.
He wound up leading the final 138 laps after passing Johnson, and pulled away after a restart with 52 laps to go, another with 12 laps left and a then a two-lap sprint to the end.
The final restart was for debris on the track, which he thought curious.
"It was there in turn one," he said of the debris. "However it was there at Lap 100. It was. I'm not kidding you. It was there. I don't know if there was another piece somewhere."
Johnson said he wished he had the car to reprise his bump-and-run pass of the spring.
"If I had a chance to pass him and to get to his bumper and work him over, I would have," he said. "But I wasn't going to come in with the second-place car and take a cheap shot and pass him that way. If I felt like I had a car to win the race, I would have been up there leaning on him some."
Instead, he protected his position and his points lead.
Seeking his record fourth consecutive series title, Johnson extended his lead over Hendrick Motorsports teammate Mark Martin from 90 to 118 with four races remaining. Jeff Gordon, also of Hendrick, remained third, but saw his deficit grow from 135 to 150.
"I would have liked to have been hooked up better, but we couldn't get it and we fought tooth and nail for everything we could get," Martin said.
Hamlin's second career victory on the 0.526-mile oval kept the Virginia racer as the only driver besides Johnson to win on the paper clip-shaped track in the last seven races.
He led three times for a race-high 206 laps, while Johnson led 164 circuits.
Hamlin, who seemingly dominates just as often as Johnson at Martinsville, but without the finishes to show for it, showed he had Johnson's number when he passed him on Lap 363.
He then pulled steadily away, deftly moving around lapped traffic and opening a lead of more than 4 seconds during a long green-flag run. With just over 70 laps to go, radio chatter indicted that the teams were fast approaching pit stops under green, a Martinsville rarity.
The stops started among drivers not in contention, and then Montoya and Johnson headed in, with Hamlin stopping two laps later. He emerged with his nearly 4-second lead intact.
The victory was Hamlin's third of the season, a small consolation because after saying he felt like the man to beat in the 10-race Chase for the championship, he had faded to 11th.