He will next face Spain's Roberto Bautista Agut, who beat American Mackenzie McDonald 3-6 6-4 6-1.
The 14-time Grand Slam champion missed six months of competition in 2017 and had elbow surgery at the start of this year.
Federer's large cheering squad - decked out in matching red T-shirts - looked on in disbelief as Medvedev grabbed the second set.
The 22-year-old then put the Swiss under immediate pressure on his serve in the deciding set, only for an uncharacteristically flustered Federer to hold.
World number one Rafael Nadal, beaten by Federer in last year's final of the Masters 1000 showpiece, is absent with injury.
Zverev is playing in Shanghai without his coach, Ivan Lendl, who was in Beijing last week.
Three days on and the 30-year-old continues to be under the weather, but just about did enough to see off Gasquet 7-5, 7-6 (9/7). But I think I played with all my strength in the important moment of the match.
In Facebook’s massive breach, the hackers’ friends were the first victims
Last month, Facebook launched an investigation with the FBI and released a statement in response the the breach. Below the notice about what information was hacked, Facebook apologizes for the security breach .
Del Potro will face Borna Coric in the next round.
Though the world No. 28 is a former finalist at this tournament, Federer was expected to storm through but he stuttered to a 6-3 2-6 6-4 win over the Spaniard.
The top-seeded Swiss, who is fighting to defend the title and his world number two spot, broke in the fourth game and sealed the opening set with a crisp backhand volley.
Number two seed Djokovic nailed his place in the last-16 on Tuesday with a convincing victory over Jeremy Chardy.
Basilashvili arrived in Shanghai after beating del Potro in Beijing to win his second career title.
"I feel like I'm where I want to be", Federer said.
"Probably the hardest-hitting player I have ever played against", said Zverev.
Wang Qiang, ranked 24th, and 40th-placed Zhang head a list of four Chinese players in the current top 100, and there are 11 in the top 200 including 17-year-old US Open junior champion Wang Xiyu.