Flaherty (5-6) allowed three hits with seven strikeouts and one walk in six innings. It marked the first time he completed six innings since July 3, when he surrendered four runs in six against the Arizona Diamondbacks.
Carpenter hit a solo home run in the fifth inning -- his career-high 29th of the season, tying him with Colorado's Nolan Arenado for the NL lead -- and Jedd Gyorko singled to drive in a run later that inning to give the Cardinals a 2-0 lead.
"(Leading the National League in home runs) is definitely not something I've ever really thought about," Carpenter said. "It's just a crazy stretch. There's not much thought behind it other than I'm just trying to hit the ball hard. I'm really not trying to hit home runs. It just happens."
St. Louis took the final two games of the three-game series to move past Pittsburgh into third place alone in the NL Central.
"Just good baseball," interim Cardinals manager Mike Shildt said. "It started on the mound. Jack was really, really tough through six innings. Offense competed well. ... Some tough at bats, good at bats, and good pitching all the way through."
Bud Norris retired the side in order with two strikeouts in the ninth for his 21st save.
Flaherty recovered after giving up four runs on five hits in 5 1/3 innings of a 6-3 loss to the Colorado Rockies in his last start.
He got out of trouble twice on Sunday, first when the Pirates had runners on second and third with one out. Corey Dickerson struck out and Starling Marte grounded out to end the third inning. With runners on first and third two innings later, Flaherty forced Jordan Luplow into an inning-ending double play.
"That was more where I kind of wanted to be at," Flaherty said. "It was nice to make adjustments from the last time out, the last few times out, just to try to get ahead. Once it was get ahead, it was stay ahead."
Carpenter's home run to right with one out in the fifth ended Trevor Williams' streak of 21 1/2 innings without allowing a run. That stretched back to July 6, when Williams (9-8) gave up three runs in the third inning of a 17-5 loss to the Philadelphia Phillies.
"At that point of the game, you are upset they are up 1-0. That's what I was worried about," Williams said. "Knowing how good Flaherty was throwing and how well he was executing his pitches, it was tough. It's unfortunate that that one blink cost the game today, but that's kind of how baseball is right now."
After the homer, Williams surrendered consecutive singles to Yadier Molina and Jose Martinez before Gyorko scored Molina with a single to left. Williams allowed those two runs on nine hits with three strikeouts in five innings.
"Now we go on the road and it's (against) good teams," Pirates manager Clint Hurdle said. "I just love the fact that we are in the hunt. Time to go. We would have loved to won more games here. We did not. Now, it's time to go put a better package together on the road."
Pirates: RHP Casey Sadler was recalled from Triple-A Indianapolis. RHP Alex McRae, who allowed three runs on five hits in 3 1/3 innings of relief Saturday, was optioned to Indianapolis.
Pirates: Dickerson started in left field for the first time since missing eight games with left hamstring discomfort. He hit leadoff and was 0 for 3 with one walk. ... INF David Freese did not play because of a right forearm contusion suffered Friday.
Cardinals: RHP Luke Weaver (6-9, 4.75) will attempt to keep his string of impressive performances going when he faces the Miami Marlins on Monday. He has surrendered three runs or fewer in five straight starts since giving up eight in an 11-4 loss to the Atlanta Braves on June 30.
Pirates: RHP Joe Musgrove (4-5, 3.63) will look for a third straight quality start when he takes the mound against the Rockies on Monday. In his last two starts, Musgrove has allowed three combined runs on 10 hits in 14 innings.
More AP baseball: https://apnews.com/tag/MLBbaseball
Phillies still sizing up Bryce Harper, Manny Machado, other free agents
Phillies president Andy MacPhail, who continues to size up free agents such as Bryce Harper and Manny Machado, says "all of our ownership group wants to win."
Roberto Osuna to close for Houston Astros this season
Roberto Osuna will close for the Houston Astros in the right-hander's first full season with the team.
Danny Farquhar takes next step in comeback, throws bullpen session for Yankees
Danny Farquhar, who suffered a brain hemorrhage from a ruptured aneurysm last April, took a massive step toward pitching again the majors, throwing a bullpen session with the Yankees on Friday.
Scott Servais: 'Overzealous' offseason behind Mallex Smith's sore shoulder
Mariners center fielder Mallex Smith will be slowed at the start of spring training due to a sore shoulder that manager Scott Servais attributes to "overzealous" offseason throwing.
Mets OF Yoenis Cespedes thinks he will play in 2019
Mets OF Yoenis Cespedes says he thinks he will be able to return this season following surgery on both heels last summer, but he doesn't know when he will be ready to play.
Tigers beat Michael Fulmer in final arbitration case; players win 6-4
Though pitcher Michael Fulmer lost his salary arbitration case against the Tigers and will make $2.8 million in 2019, players finished hearings with a 6-4 advantage in decisions.