The defending champion took a seven-shot lead into the final round at Bethpage Black and remained six ahead with eight holes to play, only to see his lead cut to one after four consecutive bogeys.
Johnson dropped shots on two of his last three holes to close out a one-under 69 and set the target at six under, as Koepka stuttered home with a four-over 74 to secure a two-stroke win.
The wire-to-wire victory is Koepka’s fourth major win in eight starts and sees him leapfrog Johnson as world No 1, with the 29-year-old becoming the first player to win both the US Open and PGA Championship in back-to-back years.
Koepka chunked his chip from thick rough at the first and missed his 10-footer to save par, as playing partner Harold Varner pitched to three feet to set up an opening birdie to cut the lead to five.
Varner’s hopes ended with back-to-back double-bogeys from the third, while Johnson – playing in the group ahead – holed from close-range at the fourth to move into outright second.
Koepka made a two-putt birdie at the par-five fourth but saw his advantage fall to four when Johnson rolled in from 10 feet at the sixth and fired his approach at the ninth to tap-in range.
A two-shot swing saw Koepka end a run of pars with a three-foot birdie at the 10th and Johnson drop a shot at the 11th, only for the world No 3 to bogey his next two holes after two wayward tee shots.
Koepka hit a wild drive at the par-five next and three-putted from just off the green for a third successive blemish, allowing Johnson to get within two after holing an 11-foot birdie at the 15th.
The lead temporarily became one when Koepka flew the green off the 14th tee, only for Johnson to double the cushion after failing to get up and down from thick rough to save par at the 16th.
Koepka’s bogey run ended with two-putt pars over his next two holes, while Johnson dropped a shot at the 17th and holed from six feet at the last to set the clubhouse target at six under.
A three-putt bogey on the 17th left Koepka with a two-shot cushion to the last, where he had to pitch out from the thick rough before firing his approach to five feet and holing the par putt for victory.
Jordan Spieth’s Grand Slam bid finished in a share of third alongside Luke List, Patrick Cantlay and Matt Wallace, while Rory McIlroy and Shane Lowry both posted closing 69s to end in a share of eighth.