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 a wreck-filled Sprint Cup race at newly paved Watkins Glen International.

It’s the first road-course win of Hamlin’s career and atoned for his stunning loss on the road course at Sonoma in June, when he was beaten by Tony Stewart after being bumped out of the way 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 virtually flawless at The Glen and 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,” Hamlin said. “Hoping for those caution laps that we needed. That allowed us to make it on fuel.”

That Hamlin was 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 with the end in sight.

The sixth caution flew on Lap 78 for a blown engine and erased a 5-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, a 90-degree downhill right-hander, 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.”

Hamlin held a slim lead over Truex and Keselowski as the three ran nose-to-tail entering the final turns of the race. Keselowski spun Truex in the last turn and Hamlin coasted to the victory.

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

“We got ourselves in position to win the race, but I didn’t have the speed to make it happen,” Logano said.

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 accident. Earnhardt is recovering from a concussion and will miss at least two more races.