"It's an unreal feeling for me. This is just a perfect moment," said Svitolina, who will face Czech Republic's Petra Kvitova in the quarter finals.
Williams was visibly frustrated throughout the match as she struggled to find her range, making 37 unforced errors compared to 17 by Svitolina.
Late in the second set, Williams served five double faults in a single game, which drew gasps from a shocked audience.
Double shock for Serena
Just days earlier, Serena and sister Venus, the three-time women's doubles Olympic champions, crashed out in the first round in one of the biggest upsets so far in women's tennis.
The top seeds fell 6-3 6-4 to Czech pair Barbora Strycova and Lucie Safarova, ending any hopes of claiming a third successive Olympics doubles crown after they won gold in Beijing eight years ago and again at London in 2012.
The sisters won their first doubles gold in Sydney but a knee injury prevented Serena from playing at Athens in 2004. They have also won 14 women's doubles Grand Slam titles, to go along with a combined tally of 29 major singles crowns.
Both women have also won Olympic gold medals in singles play, with Venus triumphing in Sydney and then Serena becoming champion in London.