Clint Bowyer spun off Turn 4 with a tire issue with less than two laps remaining in the 80-lap stage, which locked Hamlin in with a stage win as there was no time to return the green-flag conditions.
Keselowski ended up second, Michael McDowell third, Jimmie Johnson fourth and Austin Dillon rounded out the top-five.
The last laps were chaotic as four different drivers experienced tire issues while several others were making last-minute green-flag stops for fuel to make it to the finish.
Austin Dillon, who started on the pole, lead the first seven laps until Johnson made his way around him on the outside on Lap 8 to take the lead for the first time.
On Lap 12, NASCAR threw a caution for a lightning strike in the area of the speedway. The red flag was immediately displayed as severe thunderstorms moved into the area with heavy winds.
At the time of the caution, Johnson continued to lead followed by Kevin Harvick, Ryan Blaney, Austin Dillon and Kyle Larson. The race returned to caution after a 3-hour, 18-minute delay.
Several drivers elected to pit but Johnson remained on the track and in the lead when the race returned to green on Lap 17. He was followed by Austin Dillon, Daniel Hemric, Alex Bowman and Alex Almirola.
Austin Dillon powered around Johnson to retake the lead on Lap 18 as Harvick moved into second.
Harvick quickly ran down Dillon and took the lead for the first time in the race on Lap 20.
Kyle Busch tagged the wall off Turn 4 on Lap 23 but was able to continue on and no caution was called.
After 30 laps, Harvick maintained a small lead over Blaney with Johnson running in third. William Byron was fourth and Keselowski fifth.
With 35 laps remaining in the first stage, Harvick’s lead over Blaney had grown to just over one second as Johnson continued to run in third, more than 3 seconds behind the leader.
Some of the cars who stayed out when the race restarted were the first to make their pit stops with Bowman the first to drop on pit road on Lap 48. Austin Dillon and Kurt Busch followed shortly afterward.
Several more drivers, including some who did pit after the rain delay, made green-flag stops by Lap 55. Hemric was penalized for an uncontrolled tire during his stop and had to make a pass through penalty.
By Lap 57, Chase Elliott and Johnson also made their stops.
With 20 laps left in the first stage, Harvick continued to lead over Blaney followed by Byron, Hamlin and Larson.
With eight laps to go, Harvick began heading down to pit road for a green-flag stop but also had a right-rear tire go down.
After 75 laps, Jones inherited the lead followed by DiBenedetto and Kyle Busch.
Jones and Kyle Busch dropped to pit road with four laps remaining. Blaney also made a second pit stop with a flat tire.
On Lap 79, Bowyer spun off Turn 4 with a flat tire to bring out a caution which locked Hamlin into the lead as there was no time to return to green before the conclusion of the stage.
Byron was forced to start the race from the rear of the field since his team made an engine change in his No. 24 Chevrolet over the weekend.
Corey LaJoie also started from the rear of the field after his No. 32 Ford made a transmission change.