Federer eventually overcame a leg injury and his American opponent 6-3 2-6 2-6 7-6 (10-8) 6-3 in three hours, 31 minutes on Rod Laver Arena.
But Sandgren squandered seven match points in the fourth set, including four in an extraordinary tie-break.
We take a closer look at the match points Federer saved.
First match point: Federer serving at 4-5, 40-AD in fourth set
With a second serve to play with, Sandgren gets into the point and a deep backhand is returned by Federer. But the American pulls the trigger too early with his next chance, sending a backhand into the net as he tries to go down the line.
Second match point: Federer serving at 4-5, 40-AD in fourth set
Another second serve to aim at, Sandgren gets into the point, but it is Federer dictating before the American sends a tame forehand wide.
Third match point: Federer serving at 4-5, 40-AD in fourth set
Sandgren manages to get into the point despite a good Federer serve down the middle before hitting the tape with a forehand from behind the baseline.
Fourth match point: Federer serving at 3-6 in fourth-set tiebreak
Another Federer second serve, Sandgren controls the first part of the point from the baseline. However, he allows the Swiss great to work his way back into it before putting a backhand into the net as he tried to change direction by going down the line.
Fifth match point: Federer serving at 4-6 in fourth-set tiebreak
Federer lands an excellent serve that Sandgren cannot return.
Sixth match point: Sandgren serving at 6-5 in fourth-set tiebreak
Finally an opportunity on serve, Sandgren attacks but just cannot do enough with a backhand volley. He reaches for a forehand volley but Federer has an open court to play into to level the tie-break.
Seventh match point: Federer serving at 6-7 in fourth-set tiebreak
Once again, Federer misses a first serve. They rally backhand-to-backhand as Sandgren shows good depth before Federer eventually changes the direction during a 19-shot point. But it is a slice that undoes Sandgren, whose return goes halfway up the net.