Townsend hasn’t won a singles match at Wimbledon since 2019, but she and Katerina Siniakova won the women’s doubles title together two years ago and her superb touch and stroke helped her level the match.
Swiatek left the court after the second set, in which she made 16 unforced errors, but took time to find her rhythm upon her return.
The 12-minute first game of the deciding set was key, with Swiatek fending off four break points and overcoming three double faults to hold serve.
Townsend, watched by Siniakova and good friend Naomi Osaka, kept in touch, but Swiatek broke into a 5-2 lead with a forehand passing shot to take the win.
“You can see the relief on Iga’s face. It was a very intense match,” former Wimbledon champion Marion Bartoli told BBC 5 Live.
“The moment when everything changed happened in the very first game of the third set. This was absolutely important because she was really losing momentum in this match.
In the second round, Swiatek will face Czech Karolina Pliskova, the 2021 Wimbledon runner-up.
American Anisimova beat Macedonian qualifier Lina Gjorchescu 6-3 6-2 to take the lead, while 2024 finalist Jasmine Paolini fought back to beat Robyn Montgomery 0-6 6-4 7-5.
However, eighth seed Elina Svitolina lost in the first round at SW19 for the first time since 2018, when she was beaten 7-5, 6-2 by Ukrainian Daria Snigur.