Stephens broke serve three times in the opening set against the 2002 and 2013 champion.
Williams, the 19-times grand slam winner, broke serve twice in the second set to level.
Stephens then faded away as Williams broke for a 3-2 lead in the decider before finishing the match on Stephens’ serve with a dipping crosscourt backhand winner.
She next faces 17th seed Sara Errani of Italy for a place in the semi-finals.
Williams, who hit 43 unforced errors, has had to work hard to make progress this year and labelled her performance unprofessional after defeating Germany’s Anna-Lena Friedsam in the second round.
She also dropped the first set in the third round against former world number one Victoria Azarenka.