Photo: Don Emmert/AFP
It was the Australian Open and Wimbledon champion's 17th win in 17 matches against Youzhny in a rivalry stretching back to 2000.
Five-time US Open champion and third seed Federer had also needed five sets to see off US teenager Frances Tiafoe in the first round.
The 36-year-old, who now has 80 match wins at the US Open, will face 35-year-old Feliciano Lopez of Spain for a place in the last 16.
His record against Lopez is just as solid — 12-0.
“It was quite a lot of fun out there — I feel quite warmed up by now,” said Federer who fired 63 winners and 68 unforced errors.
“Looked liked Mikhail was cramping at the end and that was hard to watch.
“We played in the doubles here back when we were juniors and have met loads of times. This was probably the best match we have played against each other.”
There was no sign of the drama to come when Federer sprinted through the opener in just 25 minutes on the back of two breaks of serve on the Arthur Ashe court.
But Youzhny held firm with a break as the Swiss star served for a two-sets lead in the 10th game and levelled the match with a steady tie-break.
Federer appeared increasingly sluggish, a possible legacy of his recent back problems, and despite saving two set points in the ninth game, Youzhny pounced in the next to claim the third set.
The momentum swung again in the fourth when Youzhny, just a year younger than Federer at 35, required a medical timeout.
Federer levelled the contest as he went into back-to-back five-setters in the opening two rounds of a major for the first time in his career.
Youzhny began to cramp and was broken in the sixth game as his challenge fizzled out.