Kevin Harvick won his second consecutive race -- and in the process may have become the most unpopular man in Virginia.

Harvick passed Dale Earnhardt Jr. for the lead on Lap 497 of 500 and pulled away to win the Goody's Fast Relief 500 Sprint Cup race Sunday at Martinsville Speedway. Earnhardt ran second, extending his winless streak to 99 races.

The victory was Harvick's second of the season and his first at Martinsville. Harvick has 16 victories in the Cup Series.

Earnhardt had muscled past third-place finisher Kyle Busch on Lap 480 and held the lead until Harvick made the winning pass in Turn 2.

Juan Montoya ran fourth, followed by Jeff Gordon, Matt Kenseth and pole-sitter Jamie McMurray. David Ragan and Clint Bowyer were eighth and ninth, respectively. Mark Martin finished 10th in his 800th race in the Cup Series.

"I hate to be the bad guy here, but we're in it to win it," said Harvick, who ended Denny Hamlin's string of three consecutive victories at Martinsville and became the first driver other than Hamlin or Jimmie Johnson to win at the .526-mile short track since Tony Stewart on April 2, 2006.

Harvick's pass accounted for the last of a track-record 31 lead changes, but it was one Harvick knew would be unpopular in some quarters.

"I could see people going crazy in Turn 2 when he took the lead from Kyle, and I was catching him, and I'm like, 'Man, I'm going to be the bad guy here,' " Harvick said. "But I've got to do what I've got to do.

"To race with Dale Jr. and win the race at Martinsville, I think it's pretty cool. ... I know the fans want to see him win -- I want to see him win. It would be great for the sport, and I think today went a long way to showing how competitive they can be, racing for wins.

"That's what we need. We all need him to win, but I'm not going to back down."

Before Harvick spoiled Earnhardt's party, the driver of the No. 88 appeared likely to take advantage of astute pit strategy on the part of crew chief Steve Letarte. A two-tire call on Lap 205 got Earnhardt out of the pits in second place. Under a caution on Lap 373, Letarte brought Earnhardt to pit road for four tires and fuel without losing a position, even though the eight cars ahead of him stayed out.

"We had the opportunity there to win a race, and I'm disappointed that I didn't get the job done," Earnhardt said. "It'll probably bother me more and more as the night goes on -- I'll probably think about it a million times, what I could have done differently -- but, you know, if I know what's best for me, I should probably have a good attitude about what happened today.

"I'll probably go into the next race and use it as momentum and confidence, as any other good driver would do, instead of worrying about how close we came. I should be thankful and grateful for the opportunity I had today and the opportunity I got to work with the team I'm with, and to even be here competing.

"Steve's a good crew chief, and he's going to give me more opportunities like this, I feel."

Busch took the series lead by five points over Carl Edwards, who finished 18th.

The race was red-flagged for almost 25 minutes to repair the Turn 3 wall after Martin Truex Jr. rammed the SAFER barrier. The throttle on Truex's No. 56 Toyota stuck wide open, and Truex T-boned fellow Toyota driver Kasey Kahne before slamming the wall.

Paul Menard's streak of five consecutive career-best finishes ended when he failed to better his previous best at Martinsville: 13th. Contact from Brad Keselowski's Dodge spun Menard's Chevy in Turn 1 on Lap 241, and Menard never recovered. Ultimately, he retired to the garage after 261 laps, in 38th place.