KIAWAH ISLAND, S.C. — Weathering a riveting, roller-coaster test of nerve over five hours, Phil Mickelson, who will turn 51 next month, won the P.G.A. Championship on Sunday to become the oldest golfer to win a major championship. The record was previously held by Julius Boros, who was 48 when he won the 1968 P.G.A. Championship.Mickelson shot six under par for the tournament, finishing two strokes ahead of the runners-up, Brooks Koepka and Louis Oosthuizen.Mickelson becomes the latest in a growing group of sports stars who have defied traditional retirement ages for athletes and proved that championships can still be won in careers that last into middle age. Mickelson has followed the lead of Tom Brady, who won his seventh Super Bowl title three months ago at 43. Serena Williams has remained consistently in the hunt for elite titles at 39, an age that historically has seen tennis players recede to a senior circuit. Tiger Woods, although seriously injured in a car crash in February, won his fifth Masters tournament two years ago at 43.“I hope that this inspires some to just put in that little extra work, because there’s no reason why you can’t accomplish your goals at an older age,” Mickelson said after his round. “It just takes a little more work.”Mickelson has been among the most popular American golfers for three decades, and the final scene of his Sunday triumph made it obvious that his appeal had not waned.At the final hole, Mickelson rocketed his tee shot into the gallery left of the fairway, but he lofted a 9-iron from the rough to within 16 feet of the hole as the crowd roared its approval. He walked toward the green shaking his left fist above his shoulder. As he did, he was enveloped by hundreds of fans, who surged past security guards and the police to celebrate alongside him.Hugged, jostled and patted on the back, Mickelson needed several minutes to walk the final 50 yards to the 18th green. With spectators chanting his first name, he finally emerged to a green encircled by the crowd. Two putts sealed his victory.Mickelson later called the experience “slightly unnerving but exceptionally awesome,” and said he would “cherish it for my entire life.”Mickelson’s achievement, his sixth major title, could prove to be a bookend to three decades in golf’s spotlight. A four-time college all-American, he won his first professional tournament while an amateur and was unable to cash its hefty check. After turning pro, he racked up victories on the PGA Tour, but soon became better known for his failure to win his first major championship.Mickelson has also had the misfortune of playing most of his career in the shadow of the superstar Woods, who won six major championships before he was 26. But Mickelson did not break through until he was 33, when he claimed the 2004 Masters during his 13th year on tour. Two other Masters championships followed, in 2006 and 2010, as well as a victory at the 2005 P.G.A. Championship, but there were also frequent, dispiriting setbacks, including six second-place finishes at the United States Open, American golf’s national championship. Before Sunday’s victory at the treacherous Ocean Course at Kiawah Island, Mickelson had not won a major since the 2013 British Open.Mickelson, however, has remained a fan favorite, in part because of a daring style of play and because of his Everyman physique, which stands in stark contrast to the fit, muscular bodies of the modern golfer best typified by Woods. Known as Lefty because he swings at a golf ball left-handed — even though he is naturally right-handed — Mickelson has spent decades comfortably engaging with golf galleries, often using a smile and a thumbs-up, a gesture he learned from Arnold Palmer.Mickelson’s rush up the leaderboard at this P.G.A. Championship was not foreshadowed by his recent performances. Since missing the cut at last year’s U.S. Open, his best result on the PGA Tour had been a tie for 21st. He finished outside the top 50 nine times.Playing in dark sunglasses and with an air of calm, Mickelson began Sunday’s round with a one-stroke lead over Koepka, who finished the event in a tie for second place with Oosthuizen at four under par.But long before Mickelson’s triumphant outcome was certain, he was locked in a tense, topsy-turvy battle, first with Koepka and then in the closing holes with Oosthuizen.While Mickelson appeared to have a comfortable four-shot lead over the field with six holes to play, his second shot at the 13th hole hooked into a water hazard, which led to an unsettling bogey. On the par-3 14th hole, Mickelson came up short and right of the green. He chipped to eight feet, but missed the par putt for another bogey.Almost simultaneously, Oosthuizen made two steadying pars at the 14th and 15th holes to pull within three strokes of Mickelson, who was at six under par for the tournament. Mickelson made par at the 15th hole, but Oosthuizen birdied the par-5 16th hole — his eagle putt narrowly missed — to cut Mickelson’s lead to two strokes.Mickelson responded with a par on the 15th hole, then hit a towering 337-yard drive to the middle of the 16th fairway. His second shot bounced on the green but skipped off the back. A dicey chip nestled next to the hole for an easy birdie, and Mickelson headed to the hardest hole on the longest golf course in major championship history with a three-shot lead.Mickelson’s 6-iron from the 17th tee took a big bounce just to the left of the hole and trundled into knee-high grass behind the green 60 feet from the flagstick. After several minutes of deliberation, he wedged his ball safely on the putting surface, where he two-putted for a bogey that still kept him two strokes in the lead.Hours earlier, in the middle of a sunny, humid day along the South Carolina coastline, Mickelson had taken a gut punch to his chances when Koepka wrested the lead from him on the opening hole. In a matter of minutes, a three-putt bogey by Mickelson and a Koepka birdie had reversed the names atop the leaderboard. But Koepka gave away the advantage with a double bogey on the second hole, even as Mickelson continued to struggle with his accuracy.His brother and caddie, Tim, pulled Phil Mickelson aside after the sixth hole and gave him a mild scolding.“Tim said, ‘If you’re going to win this thing, you’re going to have to make committed golf swings,’” Mickelson said, adding that he had been too passive. “It hit me in the head — I have to swing committed. The first one I made was the drive on 7.”Mickelson birded the seventh hole, which Koepka bogeyed. By the turn, Mickelson had pushed ahead by two strokes.When Mickelson’s final putt dropped into the cup on the 18th green, the brothers embraced for several seconds.