INDIANAPOLIS — Kasey Kahne survived a crash-marred Brickyard 400 on Sunday for his first NASCAR Cup victory in nearly three years.

The Hendrick Motorsports driver won under caution in the race that took more than six hours to complete, finally finishing in fading light after 167 laps and double overtime.

Brad Keselowski was second.

Afterward, Kahne went to Indianapolis Motor Speedway's infield medical center. Series officials didn't say what he was being treated for but other drivers said the temperatures inside the cars topped 130 degrees.

It was a wild day with Kyle Busch and Martin Truex Jr. — driving the two fastest cars — going out in a crash with 49 laps to go.

That was only a prelude the nutty final 20 laps that included five crashes, two of which brought out red flags.

Busch looked as if he would make history as the first driver to win three consecutive Brickyards when he led the first 71 laps — and 87 overall. But when Truex's car slid up the track, both cars hit the wall.

That opened the door for Kahne, who had struggled all season. And as the crashes happened behind, Kahne overcame muscle cramps to hold on for his 18th career victory and first since Atlanta in August 2014.

The race was red-flagged with 10 laps to go after Kurt Busch, Erik Jones and Clint Bowyer collided coming out of the fourth turn.

On the ensuing restart, Kyle Larson hit the wall on the front straightaway to bring out another caution.

Things went awry once more when Jimmie Johnson, Keselowski and Kahne tried to go three-wide through the third turn on what was supposed to be the second-to-last lap. But Johnson's smoking car spun and slammed into the wall, forcing overtime.

"I wouldn't call it an absurdity, I just think it was a crazy race," Keselowski said. "There was some crazy strategy."

And after Trevor Bayne's car got sideways with two laps left in the first overtime, another red flag came out and another overtime began.

But the race still wasn't over.

With the sun setting and no lights at the track, Kahne barely made it to the overtime line as another multicar pileup occurred behind him to bring out the last of the 14 cautions to seal the win.