Playing under the roof at the Arthur Ashe Stadium for the first time, the five-time champion was taken the distance by the world number 70 before eventually prevailing 4-6 6-2 6-1 1-6 6-4.
It was a dream start for the home underdog as he broke Federer in the very first game and went on to hold serve throughout the opener to claim it 6-4.
Federer's performance saw questions raised over his fitness just two weeks after he pulled out of the Cincinnati Masters with a back injury, as he dropped a set in a first-round match at Flushing Meadows for the first time since 2003.
The Australian Open and Wimbledon champion quickly bounced back, though, and it took him just an hour to turn things around with convincing victories in the next two sets.
The quickest set of the match saw Tiafoe force a decider, though, as he rallied in the fourth to win 6-1 in just 24 minutes.
The American had the chance to force a tie-breaker after one break apiece in the final set too, but he lost the crucial game when serving to stay in the match as Federer finally sealed his progress with his third match point.