Fourth-seeded Alexander Zverev of Germany also advanced to the quarterfinals by pasting Alex de Minaur of Australia 6-1, 6-4.
On account of Federer ten aces and three breaks.
The Italian battled his way past Hyeon Chung on Wednesday, coming from a set down to win 4-6 7-6 (7-5) 7-6 (7-5).
The third set remained on serve until, at 4-4, Federer claimed his first break since the start of the second set, and he capitalized to secure another hard-fought win having also beaten Daniil Medvedev in three sets on Wednesday.
But the unseeded Spaniard earned a piece of personal history after that, winning the second set - the first time he had ever done that against the Swiss maestro. "So if it brings yourself to think I am ideal; the truth is that I am not", added the Swiss maestro.
Djokovic will play South Africa's seventh seed Kevin Anderson.
But soon, Medvedev settled down and got the break back nearly immediately to level the set at 2-2 as Federer started making a lot of errors, especially on the forehand side.
But at 4-4, the Swiss moved up a gear and played a brilliant, attacking return game to get the break at a crucial juncture and closed out the opener 6-4.
Earlier, 10th-seeded Stefanos Tsitsipas beat Karen Khachanov 6-4, 7-6 (8) - the second straight year that the Greek player has defeated Khachanov in the second round in Shanghai. "At this point in my life, I want to do what I really like to", said Federer. "I do not want to paint myself as a flawless person". Federer has surprised everyone on the tour by improving his game as he has turned older.
Normal order appeared to have been resumed when the Swiss comfortably claimed the first set against Bautista Agut.
The top seed and 20-time Grand Slam victor emerged from another nervy battle to eventually defeat the 28th-ranked Spaniard 6-3, 2-6, 6-4.
The number one seed and defending champion was pushed all the way by the surprise victor of the Japan Open, but broke in the ninth game of the deciding set before serving out to reach the last 16.