Going into the final day at the Bethpage Black course, the main question was how many shots Koepka would win the tournament by.
Johnson was among four golfers who started seven behind the leader but was the only one to emerge as a serious challenger.
The pair had tied for second behind Tiger Woods at this year's Masters.
But just as it seemed Koepka had pulled away again, he imploded.
He missed a short putt on the 17th for bogey reducing his lead to two shots over Johnson, who had just completed his final round.
Koepka drove way left into the rough on the 18th, although it did leave him with a good angle to the hole.
He played it safe out of the long grass, followed by a superb approach which left him a short putt for victory.