TALLADEGA, Ala. (AP) — To win the championship this year, Team Penske told Brad Keselowski he had to win at Talladega Superspeedway.

So he did.

Again.

Keselowski earned an automatic berth into the third round of the playoffs with a victory Sunday at Talladega, where he considered himself lucky to finish after a sloppy day for NASCAR.

"I survived," Keselowski said. "I feel like only eight cars finished the race. It was one of those crazy days ... a lot of attrition."

There were just 14 cars running at the conclusion, and only four were playoff drivers. The race was red-flagged three times for nearly 35 minutes.

By avoiding the carnage, Keselowski won for the fifth time at Talladega and gave Ford a season sweep of the four restrictor-plate races.

Most important, though, is that he knows he's still in the hunt for his second Cup title. The victory vaulted him from 10th to second in the playoff standings.

"You'd love to be able to pat yourself on the back and say it's all skill, but there is some luck that's involved in this," Keselowski said. "When you come here, probably three out of every four races you're going to get caught up in a wreck. But the races where you have the good fortune, where you don't get caught up in a wreck or you don't have something break or any of those things, you have to take those races, run up front and win them.

"That's what we've been able to do."

Talladega was the perfect place for Keselowski to take the stress off before next week's elimination race at Kansas. Crew chief Paul Wolfe told Keselowski he was going to need to win Sunday because the Fords haven't run as well as the other playoff contenders on intermediate tracks, so this was his shot.

Wolfe didn't doubt the driver could deliver. When the elimination format was rolled out in 2014, Keselowski had to win Talladega to advance. The race this year was moved to the middle event of the second round, so eliminations weren't at stake, but Keselowski needed a big day.

"As we tried to understand what we need to do in each round and look at our strengths and weaknesses ... I looked at Talladega and this round as an opportunity," Wolfe said. "This was going to be our path to make it to Homestead, and that was going to be winning Talladega.

"When he gets in these situations, you're going to get the most out of him. You've got to have a little bit of help, lucky breaks, whatever you want to call them, along the way, but I knew if we had that, our cars have been fast enough, and he's talented enough at these tracks that I knew we could get it done."

Keselowski was the leader when the race resumed with three laps remaining, but was passed by Ryan Newman, who stayed out front until the final lap. Then Keselowski made his move around Newman on the high side of the track to take the lead, and a crossover move held off both Newman and Penske teammate Joey Logano.

Keselowski then collected the American flag for the victory lap on his 24th career Cup win, which came in his 300th start. His first career Cup win was at Talladega in 2009.

Dale Earnhardt Jr. finished seventh in his final race at Talladega, where he and his late father combined to win 16 Cup races. Alabama declared Sunday "Dale Earnhardt Jr. Day."

"This has been a hell of a weekend for me, and I'm glad to be able to finish and finish well. That means a lot to me," Earnhardt said. "I know a lot of folks came and traveled here to see us run, so I know they're disappointed we didn't win, but I'm glad they got to see us run the whole event, and hopefully they enjoyed it."

Newman finished second in a Chevrolet and was followed by Trevor Bayne, Logano and Aric Almirola — all non-playoff drivers.

"Brad and I work really well together on these speedways," Logano said. "I was able to push him up to the lead and I thought, 'Alright, now I got you to the lead. It's my turn to get up there and try to win this thing.' Our never-quit-attitude got us a top-five out of a day that looked like we may not even finish. I'm proud of that."

Denny Hamlin finished sixth, the best of the Toyota drivers. But the manufacturer's playoff streak came to an end after Martin Truex Jr. and Kyle Busch gave Toyota wins in the first four playoff races.

Of the top 10 finishers, only Keselowski and Hamlin were playoff drivers.

The rest of the 12-driver playoff field had a rough afternoon.