Federer, playing his first Tour-level match since suffering a shock fourth-round defeat by John Millman at the US Open last month, broke Medvedev twice to take the opening set.
Fresh off the biggest title of his career in Tokyo, Medvedev saved a break point early in the second set and that gave him momentum to grab a break of his own to force a decider.
Both players failed to take their opportunities to take an early lead in the final set before two unforced errors from Medvedev handed Federer a break in the ninth game.
The 37-year-old Swiss served out the match in just under two hours to keep hopes of a third Shanghai title alive. He will next face Spain's Roberto Bautista Agut, who beat American Mackenzie McDonald 3-6 6-4 6-1.
Fourth seed Alexander Zverev edged one step closer to qualifying for next month's ATP Finals after a hard-fought 7-5 6-4 win over in-form Georgian Nikoloz Basilashvili.
The 21-year-old German fought off a cold and showed great confidence to wrap up the victory at the Qi Zhong Stadium.
Argentine Juan Martin del Potro overcame a slow start to outlast France's Richard Gasquet 7-5 7-6(7).
Third seed Del Potro won four straight games from 3-5 down in the opening set before recovering from 2-6 deficit in the second set tiebreak to set up a third round meeting with Croatian Borna Coric.
Wimbledon finalist Kevin Anderson fired 14 aces to breeze past Kazakh qualifier Mikhail Kukushkin 6-3 6-2.