Randal Grichuk drove in four runs for St. Louis, which snapped a four-game skid.
San Francisco had won seven of eight.
Wainwright (4-3) allowed one run and five hits over 6 1/3 innings. He also had an RBI double in the sixth.
The veteran right-hander tossed seven sharp innings in a 5-0 win over the Chicago Cubs last Sunday. Before that, Wainwright had given up four earned runs in each of his three previous starts.
Carpenter hit a two-run homer off Matt Cain (3-2) in the fifth. Cain gave up seven runs and nine hits in 5 1/3 innings. He is 0-4 in six regular-season starts at Busch Stadium.