Wawrinka - who has never won Wimbledon and has now failed to reach the third round for four straight years - looked on course for a routine win after fighting back from losing the first set.
But Opelka rallied to clinch a 7-5 3-6 4-6 6-4 8-6 victory in three hours and 13 minutes.
World number 19 Wawrinka started Wednesday's encounter on No.2 Court in sluggish fashion, with the American breaking serve in the 11th game.
Opelka could not keep up his form in the next two sets, however, but the 2015 boys' champion broke the Swiss star – seeded at 22 - again to force a fifth set.
And after Opelka held his nerve on serve to edge into a 7-6 lead, the 21-year-old made the most of three match points when Wawrinka sent a weak forehand shot into the net.
Wawrinka joins the likes of Alexander Zverev, Stefanos Tsitsipas and French Open finalist Dominic Thiem, in slumping out in the opening rounds.
Opelka, meanwhile, will face Robin Haase or Milos Raonic in his next match.