Manny Machado hit his 30th homer for the Padres, who had won five in a row. They currently hold the NL's No. 2 wild-card spot.
Donovan's slam made a winner of Jack Flaherty (1-1), who struck out nine in six innings. He allowed three runs, two earned, on four hits and four walks.
Giovanny Gallegos pitched the ninth for his 14th save.
The Cardinals, who had been shut out in their previous three games, got on the board when Nootbaar led off the fifth with a drive to right against Joe Musgrove for his 13th homer. That ended a scoreless streak of 31 2/3 innings by Padres starters, five outs shy of the club record.
“We know we have a great team and a great offense that’s going to turn at any point," Donovan said. "It was just a matter of who was going to do it. I’m pumped for Lars. That’s a big swing.”
Musgrove was gone after five innings and 72 pitches, and Adrian Morejon breezed through a perfect sixth to hold a 3-1 lead.
Alec Burleson singled leading off the seventh and Nootbaar drew a one-out walk, leading manager Bob Melvin to bring on Nick Martinez (4-4). Tommy Edman walked to load the bases before Donovan drove his first career grand slam deep to right-center.
Donovan said he was looking to drive a ball to the biggest part of the field and bring in a run when he connected on a 3-1 cutter. It was his fifth homer this season.
“It just kind of ran into my barrel a little more than I thought it would. I was still running hard. You never know," he said.
Asked what going through his mind, he added: “Don't miss the base. ... I'm just running hard and make sure I hit all the bases on the way by."
Pujols, who plans to retire after this season, one-hopped the wall in left for a single in the second. He popped up in the fourth and then hit a drive off Morejon that Jurickson Profar caught on the track for the final out of the sixth. He flied out to center in the eighth.
Profar drove Flaherty's second pitch deep into the seats in right for his fourth career leadoff homer and third of the season. He has 15 overall. Profar also hit an RBI double in the fifth.
Musgrove allowed one run and five hits, struck out four and walked two.
A CLIPPERS THING
Norman Powell of the Los Angeles Clippers, who went to San Diego's Lincoln High, threw out a ceremonial first pitch. Teammate Paul George attended a game last homestand and then visited the victorious Padres clubhouse. Kawhi Leonard, who played two seasons at San Diego State, attended a game with his family earlier in the season. The Clippers moved from San Diego to Los Angeles in 1984.
Cardinals: LHP José Quintana (5-6, 3.16 ERA) starts Friday night in the opener of a three-game series at the Los Angeles Dodgers. Andrew Heaney (3-2, 2.66 ERA) pitches for the NL West leaders.
Padres: LHP Sean Manaea (7-9, 5.18 ERA) is scheduled to start Friday night in the opener of a three-game series at Colorado, their last trip of the regular season. The Rockies are scheduled to start RHP Ryan Feltner (3-8, 6.05 ERA).
More AP MLB: https://apnews.com/MLB and https://twitter.com/AP_Sports