Murray was frustrated by Melzer for large parts of the match
British number one Andy Murray teetered on the brink of defeat before coming back to beat Jurgen Melzer 6-7 (5-7) 4-6 7-6 (7-5) 6-1 6-3 at the US Open.
Sixth seed Murray was two points from a dismal third-round exit before edging the third-set tie-break, snatching it with a drilled backhand down the line.
Murray, who had cut an increasingly frustrated figure, raced through the fourth set as Melzer saved his energy.
But Murray grabbed the crucial break at 3-3 and his superior fitness told.
"He was playing awesome, hitting the ball so hard and flat," a relieved Murray said afterwards.
"It was so tough for me to get a rhythm, but I felt after the third set, the momentum was with me.
"My fitness is something I've worked so hard on. Ideally, you'd rather not play matches of that length, but sometimes it happens. You've got to get through it."
Murray's fourth-round opponent is 10th seed Stanislas Wawrinka, who also flirted with a shock defeat before beating Flavio Cipolla in five sets.
While Murray beat the Swiss number two earlier this year, the Briton's form in New York is hardly cause for confidence.
After barely breaking sweat in his opening-round win, the 21-year-old struggled past Michael Llodra and found himself in dire straits against Melzer.
But Murray insisted he had not underestimated the world number 48.
"The guy played great. It happens sometimes. I'm not that good of a player that I can just blitz guys," he said.
"I said it was going to be a difficult match, and he's very talented. He's given top guys a lot of problems in the past."
Murray played a good opening set, but failed to take his chances.
Twice he had set points at 6-5 but Melzer first delivered a brave backhand down the line and then hammered down a huge serve which Murray could not return.
Murray had chances in the tie-break, taking a 4-3 lead, but it was Melzer who snatched it with a well-placed drop shot which his opponent could do nothing with.
The second set followed a similar pattern, with Murray forging four opportunities to break but taking none.
At 2-2 and break point down, Melzer flung himself at a forehand which Murray was convinced landed long - but with Hawk-Eye unavailable on Grandstand, the Scot had no way of proving it and he was left seething.
By contrast, Melzer, whose brutal hitting frequently had Murray on the back foot, earned his first break point of the match at 5-4, and took it when Murray sent a backhand volley into the tramlines.
The British number one was, by now, fuming, constantly directing his ire at his support camp, his shoulders slumped.
But he somehow rallied, firing a backhand return down the line to break for the first time - at the 10th opportunity - to take a 3-2 lead.
He could not hang on, though, as a miserable drop shot handed the break back as he served for the set, and he looked to be heading for the exit as an ace gave Melzer a 5-4 lead in the tie-break.
But Murray's serve, which would become an increasingly potent weapon as the match progressed, got him out of trouble, with two huge first deliveries giving him set point before a backhand winner earned him the set.
Having been so close to a superb win, the big-hitting Melzer tired in the fourth set, allowing it to slip away easily once he had been broken early.
The Austrian mustered one final challenge when he carved out two break points at 2-1 but Murray hammered down two massive serves and escaped.
And once Murray broke at 3-3, the energy drained from Melzer and the Briton finally sealed victory after three hours and 52 minutes.
Murray celebrated by showing off his biceps as he did after recovering from two sets down against Richard Gasquet at Wimbledon earlier this year.
That win was followed by a chastening defeat by eventual champion Rafael Nadal. Murray will hope history does not repeat itself when he faces Wawrinka.
• There was more good news for the Murray family when Andy's older brother Jamie and mixed doubles partner Liezel Huber upset sixth seeds Kveta Peschke and Pavel Vizner 7-5 6-3 in the second round.