Having lost the opening set to Garbine Muguruza, Japan’s Osaka was a break down in the second and faced two match points at 5-3 in the third.
But she won four games in a row to win 4-6 6-4 7-5 against the Spaniard, who is also a former world number one.
Williams then came through 6-4 2-6 6-4 against Belarusian Aryna Sabalenka.
The American surged into a 4-1 lead in an entertaining final set before 22-year-old seventh seed Sabalenka broke back leveled at 4-4.
But Williams held serve and then broke her opponent’s serve to clinch the win and keep alive her hopes of a record-equalling 24th Grand Slam singles title.
Williams will face second seed Simona Halep, who recovered to beat 19-year-old French Open champion Iga Swiatek 3-6 6-1 6-4.
While Osaka, who won the second of her three Grand Slams at the Australian Open in 2019, will face Hsieh Su-Wei in the last eight.
The Taiwanese 35-year-old beat 19th seed Marketa Vondrousova 6-4 6-2 to become the oldest player to make a Grand Slam quarter-final debut in the Open era.