WATKINS GLEN, N.Y. – Denny Hamlin wasn't about to get shoved aside again.

Ignoring what he described as the worst pain he has ever felt in a race, Hamlin prevailed in a four-lap dash to the checkered flag Sunday to win the wreck-filled Sprint Cup race at newly paved Watkins Glen International.

It's the first road course win of Hamlin's Cup career and atoned for his stunning loss on the road course at Sonoma in June, when he was beaten by Tony Stewart with a bang on the last turn.

"It's very hard to win these races," Hamlin said. "I only made one mistake at Sonoma, and I didn't win."

Hamlin was able to conserve enough fuel at the end, thanks to eight cautions for 20 laps. The race also had two red flag stoppages for 30 minutes.

"We executed perfectly. Didn't make any mistakes on pit road," he said. "Hoping for those caution laps that we needed. That allowed us to make it on fuel."

That Hamlin would be behind the wheel of the No. 11 Joe Gibbs Racing Toyota was in doubt before the race. He was in pain when he awoke.

"Knew I was in pretty big trouble," said Hamlin, who has undergone two knee surgeries in his career. "I was thinking under the [last] red flag let's get this over with so I can get out of this car."

The race was shaping up as another duel between Brad Keselowski and Kyle Busch in the closing laps. But a rash of late cautions shuffled Busch back. Keselowski restarted with the lead with 25 laps to go in the 90-lap race, and Busch stalked him in second as the leaders began turning the fastest laps of the race around the 2.45-mile layout.

The sixth caution flew on lap 78 for a blown engine and erased a five-second lead the two leaders had built over Hamlin, Martin Truex Jr. and Joey Logano, who was seeking his second straight sweep of the Cup and Xfinity races at The Glen.

Keselowski lost the lead on the restart when both he and Busch overdrove the first turn, and Hamlin sneaked past to take the lead before another caution flew.

"When I saw them move forward and lunge into the corner, I knew there was no way for them to get out with any sensible speed," Hamlin said. "It was my opportunity."

Logano finished second, and Keselowski, AJ Allmendinger, Stewart and Busch completed the top six.

Jeff Gordon drove the No. 88 Chevy for Hendrick Motorsports in relief of Dale Earnhardt Jr. for the third straight race and finished 14th in his 800th career start after an early crash. Earnhardt is recovering from a concussion and will miss at least two more races.

Despite a pit road penalty and a late crash, Chris Buescher remained on the cusp of making the Chase for the Sprint Cup title despite finishing 30th, one lap down. Buescher, the surprise winner last week at Pocono came to Watkins Glen six points outside the top 30 in points.