Koepka had followed a course record of 63 on Thursday with a second round of 65 to set the lowest halfway total in major championship history and took a tournament-record seven-shot lead into the last day at a blustery Bethpage.
No player has ever surrendered such a lead in a major after 54 holes and the 29-year-old remained six clear with eight to play, but bogeyed the next four holes to allow good friend Dustin Johnson to close within a single shot.
However, with the raucous New York crowd now chanting his name, Johnson dropped shots on the 16th and 17th to give Koepka the breathing space he so desperately needed.
To his credit, Koepka vitally found the fairway on both the difficult 15th and 16th holes and had the luxury of dropping a shot on the 17th before completing a closing 74 to finish eight under, two shots ahead of Johnson.
England’s Matt Wallace was four shots further back in a share of third place with Jordan Spieth and Patrick Cantlay, with Rory McIlroy and Shane Lowry among a tie for eighth on one over.
Koepka will replace Johnson as world number one on Monday and with the US PGA moving from August to May this year, he is the first player to hold back-to-back titles in two majors at the same time following his US Open wins in 2017 and 2018.
Birdies on the fourth, sixth and ninth had taken Johnson to the turn in 32 and reduced the gap to four, Koepka having made a poor start with a bogey on the first before reducing the par-five fourth to a drive and an eight-iron to set up a two-putt birdie.
With Johnson about to drop a shot on the 11th, Koepka struck what looked to be the killer blow with a stunning approach to the 10th to set up a tap-in birdie and restore a six-stroke cushion, although he had to hole from six feet on the next to avoid a double bogey.
Another bogey on the 12th saw the lead trimmed to four shots again and Koepka was fortunate to get away with a wild drive on the 13th which finished on a trampled down patch of grass where the spectators had been walking.
However, he failed to find the fairway with his recovery and came up short of the green with his third shot before three-putting from long range.
A third straight bogey meant his lead was down to three and minutes later it was just two as Johnson holed from 11 feet for birdie on the 15th.
Koepka’s mind was scrambled by this point and his tee shot on the 14th flew fully 15 yards over the green as a combination of adrenaline and a possible drop in the wind struck at the most inopportune moment.
With a fourth bogey in a row Koepka’s lead was down to a single shot but he got the help he needed when Johnson was unable to save par from over the 16th green and another bogey on the 17th effectively ended his chances.
After finishing runner-up in the Masters last month, Johnson has now finished second in all four majors with his sole success coming in the 2016 US Open.
Speaking at the trophy presentation, Koepka said: “I’m just glad we didn’t have to play any more holes, that was a stressful round of golf.
“The wind was up, DJ (Johnson) played awesome and he put the pressure on. I’m glad to have this thing (the Wanamaker Trophy) back in my hands.
“It’s incredible. I don’t even know if I dreamed of this. This is cool. I am still in shock right now, this is awesome.”