Hamlin checked up after sliding by the entrance with 54 laps left and fell behind Truex by 20 seconds. But Hamlin slowly reeled in Truex and passed him when his rival brushed the wall and got a flat tire.
Hamlin swept throwback weekend at Darlington after winning the Xfinity Series race Saturday.
Truex won the first two stages of the Cup race on Sunday, clinching NASCAR's regular-season championship and gaining the No. 1 seed heading into the playoffs in two weeks.
Hamlin led 124 laps and looked as if he was easily on the way to a second Southern 500 title after winning in 2008. Then inexplicably, Hamlin missed the entrance and seemingly threw the race to the ever-steady Truex.
"We can still do this,'' Hamlin's crew chief, Mike Wheeler, told his driver after the miscue.
Hamlin got up to Truex's bumper with three laps to go before moving low around lapped traffic to take the lead and hold on for victory.
"This is a good day all in all. So proud to be back in victory lane in the Southern 500,'' he said.
Truex rubbed the wall , blew his right front tire and stumbled home in eighth.
"Sometimes it's just not your night,'' Truex said. "Tonight wasn't our night.''
Dale Earnhardt Jr. finished 22nd -- the same as how he started -- in his final time racing at Darlington. On Friday, the track rechristened one of its suite buildings in Turn 3 as Earnhardt Towers in honor of Dale Jr. and his late father, Dale Earnhardt, who won nine times at Darlington.
It's the second time Hamlin has swept events at Darlington, winning both the Xfinity and Cup races together in 2010 and this year. Hamlin passed Joey Logano coming off the final turn to take Saturday's race.
WHO'S HOT: Kyle Busch came to Darlington off a sweep of the Truck, Xfinity and Monster Energy races at Bristol two weeks ago. Busch wound up second and led six laps on the way to his 10th top-5 finish of the season.
WHO'S NOT: Clint Bowyer had hoped to channel the spirit of Mark Martin -- Bowyer's car carried a paint scheme in tribute to Martin -- but instead Bowyer was out of the race just 18 laps in with a broken valve. Bowyer, who came in 17th, in points, is on the outside looking in on NASCAR's 16-team playoff picture and would need a last-ditch win at Richmond next week to keep going.
UP NEXT: Richmond International Raceway, Saturday. Hamlin is the defending race winner.