A near-collapse saw Koepka's record seven-stroke lead reduced to a single shot, but he withstood making four bogeys in a row on the back nine and another at 17 for an unexpectedly narrow triumph.
"This is probably the most satisfied I've been with all the majors," said Koepka. "This one's definitely at the top of the list of how emotionally and how mentally spent I am."
Koepka fired a four-over par 74 final round at windy Bethpage Black to finish 72 holes on eight-under 272 and defeat Johnson by two strokes, replacing him as world number one as a result.
"I'm just glad we don't have any more holes to play," Koepka said. "That was a stressful round of golf."
Bogeys by Johnson at 16 and 17 made the difference but Koepka made bogey at the par-3 17th, then escaped sand and weeds off the 18th tee by finding the fairway and green then sinking a six-foot putt for the victory.
"DJ played a hell of a round to come back and to grind it out," Koepka said. "He did a great job putting pressure on me, making me play some solid golf down the stretch."
The usually poker-faced Koepka admitted the moment got to him on the final hole, where he fired a fist pump after his winning putt.
"That was the most excited I've ever been in my life there on 18," Koepka said.
Thai hope Jazz Janewattananaond, who was at tied second after round three, stumbled to a 77 on Sunday for a combined score of 2 over-par-282 which placed him at tied 14th. Compatriot Kiradech Aphibarnrat followed five shots behind for a tied 47th.
Koepka, who seeks his third US Open win in a row next month at Pebble Beach, became the first man to own back-to-back titles at two majors simultaneously by capturing the Wanamaker Trophy and the $1.98 million (1.77 million euro) top prize.
"This is unbelievable," Koepka said. "I don't know if I even dreamed this. It's amazing."
The 29-year-old American became the PGA's fifth wire-to-wire winner after Hal Sutton in 1983, Ray Floyd in 1982, Jack Nicklaus in 1971 and Bobby Nichols in 1964.
Koepka joined Tiger Woods as the only back-to-back stroke-play winners of the PGA, Woods having done it in 2006-07 as well as 1999-2000.
- DJ's Runner-up Slam -
Koepka seized a tournament-record lead of seven strokes after 54 holes on 12-under par 198.
No man has led a major by so much so late and lost. But Koepka came close.
Johnson, who shot 69, shrank the margin to four shots at the turn and just one with four holes to play, only for Koepka to outlast his US compatriot.
Johnson, seeking his second major title after the 2016 US Open, was hoping to match the best final-round win comeback in PGA history, John Mahaffey's seven-shot rally in 1978.
Instead, he completed a career "runner-up" Grand Slam, having placed second at the 2011 British Open, 2015 US Open and last month's Masters.
"I'm pleased with the way I played. I gave myself a chance," Johnson said.
"The golf course played extremely difficult. The wind was really blowing. I played really well."
- Shock back-nine drama -
Koepka made the turn on 12-under, answering an opening bogey with a birdie at the par-5 fourth, while Johnson made birdies at the fourth, sixth and nine holes to reach 8-under, shrinking Koepka's lead to its lowest since Friday afternoon.
Koepka hit a gap wedge to two feet at the 10th hole and tapped in for birdie while Johnson took a bogey at 11 to stretch Koepka's margin back to six strokes.
Then came Koepka's four consecutive bogeys while Johnson birdied the 15th for the fourth day in a row, trimming the margin to a single shot as an electric atmosphere began to build among a vocal New York crowd.
"How could you not know with the DJ chants," Koepka said. "I heard everything."
Johnson missed an eight-foot par putt at 16, giving Koepka a two-shot edge, and Johnson went over the green at the par-3 17th on the way to another bogey, boosting Koepka's margin back to three shots.
"I knew I needed to do something on the last three holes," Johnson said.
Koepka ended his bogey run by parring 15 and 16, the latter on a tense three-foot putt, but missed a four-foot par putt at the par-3 17th while Johnson closed with a par, setting up the 18th-hole drama.
"The putt on 16 gave me a little bit more confidence coming down the stretch," he said. "I know I missed one on 17, but I think 16 helped me make 18."
Koepka became the first player to win his first four majors in less than two years, joined by Woods, Nicklaus and Hogan for winning four in eight starts.