Barty, the reigning Wimbledon champion, dropped serve twice in each set as 48th-ranked Sorribes Tormo made just five unforced errors to the Australian's 27 to earn a shock win.
"It's an amazing feeling, I still can't believe it," said Sorribes Tormo, who will go on to face France's Fiona Ferro in the second round.
"It's something incredible for me. It's something that I've been dreaming of all my life, being here and even more so beating the world number one. I'm super, super happy."
While Barty's bid to become the first Australian singles gold medallist in tennis ended prematurely, she and partner Storm Sanders are through to the second round of the women's doubles.
Barty could potentially come up against Sorribes Tormo again in the quarter-finals of that competition. Sorribes Tormo and Paula Badosa are in first-round action later on Sunday.