Miguel Andujar homered for the Pirates, who had their five-game winning streak snapped. Pittsburgh was looking for its first three-game sweep in St. Louis since May 1991.
Walker gave the Cardinals a 3-1 lead with his 14th homer in the third. He also doubled and scored in the seventh.
“Every once in a while, I just get a pitch that I feel like I can hit,” Walker said. “Just recently, I've been a little more selective and wait for a specific thing that I want to look for. I'm just not swinging at everything.”
Thompson (4-5) allowed three runs and seven hits on 92 pitches. He struck out six.
Giovanny Gallegos earned his 10th save in 16 chances.
Thompson rejoined the starting rotation Aug. 18 after a tough stint at Triple-A Memphis, where he compiled a 1-4 record with an 8.66 ERA.
“I was trying to add too much all at once,” he said. “I got kind of a break there in July, didn't throw for a while. It just gave me a fresh start. I try to be the same guy every day. Not too high, not too low.”
Walker became the ninth player since 2019 to reach 14 career homers before age 22. He also executed a perfect headfirst slide at home to score the final run, touching the plate with his hand.
“We're seeing him develop on both sides of the baseball,” St. Louis manager Oliver Marmol said. “Over time, he's going to be a pretty scary player.”
Pittsburgh starter Johan Oviedo (8-14) lasted just 3 2/3 innings. He gave up five runs on four hits and walked a season-high six. Oviedo was coming off a two-hit shutout of the Royals last Monday.
The Pirates, who issued nine walks, began the six-game trip with a three-game sweep in Kansas City.
“A 5-1 road trip and we played all the way through until the ninth inning today,” manager Derek Shelton said. “Proud of this group with the way they battled.”
The Cardinals are off on Monday. It will mark the first time they have not played on Labor Day in a full season since 1993.
Cardinals: LHP Matthew Liberatore allowed three hits and two runs in three innings of a rehab outing with Memphis. He is expected to rejoin the Cardinals after being placed on the 15-day injured list Aug. 22 with lower back tightness.
Cardinals: RHP Miles Mikolas (6-10, 4.66 ERA) pitches Tuesday in the opener of a three-game series at major league-best Atlanta. Mikolas is 1-4 with a 3.86 ERA in six career appearances against the Braves.
AP MLB: https://apnews.com/hub/mlb