Federer, the five-time champion and looking to end his 10-year wait to add to his total in New York, was beaten by Australia’s world number 55 John Millman 3-6, 7-5, 7-6 (9/7), 7-6 (7/3).
It was 20-time major winner Federer’s first loss against a player outside the top 50 in 41 meetings at the US Open.
He finished with 77 unforced errors and 10 double faults.
“It was very hot. I felt I couldn’t get any air and I was sweating more and more and became more and more uncomfortable,” explained Federer.
“I guess I was happy when the match was over.”
He added: “John dealt with it better — he’s from Brisbane, one of the most humid places in the world!”
It was the world number two’s earliest defeat at the tournament since a fourth-round loss to Tommy Robredo in 2013.
The result meant there would be no quarter-final showdown with old rival Novak Djokovic who will now face Millmann instead.