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.
“I have so much respect for Roger and everything he’s done for the game. He’s been a hero of mine,” said Millman.
“Today he was definitely not at his best. But I’ll take it.”
Sharapova, the 2006 champion, suffered a second successive last-16 exit in New York, blasted off court by Carla Suarez Navarro.
The Spanish player marked her 30th birthday in style with a memorable 6-4, 6-3 win over the five-time major champion whose last Slam title was in Paris in 2014.
Federer had cruised through the first set against Millman who was appearing in his maiden last-16 at the majors.
But the Swiss, who lost in the quarter-finals at Wimbledon where he was defending champion, began to look every bit the 37-year-old he is as he struggled in the suffocating humidity.
He squandered two set points in the second set and another in the third set tiebreak as the errors piled up.
Federer was 4-2 up in the fourth set but was reeled in and two back-to-back double faults sent him 1/4 down in the tiebreak. (AFP)