"That was huge for us to capitalize on that inning and really maximize that situation," said Jason Castro, who singled and scored in the rally.
Bedard (3-3) allowed seven hits and walked one, but limited St. Louis to three runs. Josh Fields, Wesley Wright and Jose Cisnero then combined to pitch two hitless innings before Jose Veras worked the ninth for his 16th save.
"They did a tremendous job," Houston manager Bo Porter said of his relievers. "Veras ran into a little bit of trouble but was able to make the necessary pitches in the ninth to get out of it and close it out."
Allen Craig homered for the second straight night and Yadier Molina had a two-run shot for the Cardinals, who lost for the fourth time in five games. They dropped into a tie with surging Pittsburgh for the lead in the NL Central.
Lynn (10-2) allowed five hits, walked four and struck out four over 7 2/3 innings for his first loss in four career starts in Houston.
Cardinals manager Mike Matheny said it looked as if Lynn lost his rhythm in the fourth.
"It wasn't a good inning," Lynn said. "The hits weren't the problem -- it was the walks. When you walk people, you get yourself in trouble, and that's what happened. I just didn't throw strikes."
Molina and Craig helped the Cardinals get off to a nice start. Molina followed Carpenter's leadoff single in the first with a drive to the Crawford Boxes in left field for his sixth homer. Craig had a leadoff drive in the fourth that bounced off the lights atop the wall in left field, extending the lead to 3-0.
But Houston responded in the bottom half. Jose Altuve and Castro got it started with back-to-back singles for the Astros' first hits of the game. Lynn then walked Chris Carter on four pitches to load the bases before sending a run home when he also walked Carlos Pena on four pitches.
Carter leads the majors with 108 strikeouts this season, but Porter was impressed with his patience in the big inning.
"His at-bats have gotten extremely disciplined from a standpoint of he's attacking the pitches that he should attack -- not expanding (the zone) as often as he was earlier in the year, and he's putting together some really good at-bats," Porter said.
Castro came home when J.D. Martinez grounded into a fielder's choice, and Brett Wallace then hit a tying RBI single. After another fielder's choice, Brandon Barnes singled in Wallace to give Houston the lead for good.
The Astros threatened again in the fifth, putting two runners on with two out, but Lynn retired Martinez to end the inning. That was the first of eight straight batters retired by Lynn, who began the day with a 3-0 record and a 2.00 ERA in three games in Houston.
Fields retired the first two St. Louis batters in the seventh before left-hander Wright struck out Carpenter.
"That was one of those situations where he pretty much just bulled his neck and said: `I'm going to throw my best against anybody that steps in the box,' and he was able to get it done," Porter said of Cisnero. "I felt like his fastball had really good life, and he actually pumped it up the last couple at 95."
Molina played first base for the second time in his career and the first since 2008. Matheny said Molina could "probably play just about anywhere on the infield." He said playing him at first base is a good way to give his legs a rest. "He's caught more innings than any other catcher," Matheny said. "His body has been able to handle it so far, but it's going to be a tough task. We're not trying to set any records here, we're just reading his body and days like this are going to be able to keep one of the top bats in baseball in the lineup while not beating him up behind the plate." ... Both teams are off Thursday. Houston then begins a series with the Angels and St. Louis starts one at Oakland. ... The Cardinals activated RHP Fernando Salas (right shoulder) from the 15-day disabled list and optioned him to Triple-A Memphis. ... Houston RF Justin Maxwell, who got a mild concussion when he hit his head on the ground attempting to make a diving catch on Tuesday, passed his concussion tests on Wednesday. He should be ready to play when the Astros start their series with the Angels Friday. ... Houston RHP Ross Seaton cleared waivers and was outrighted to Double-A Corpus Christi. LHP Wade LeBlanc also cleared waivers and was outrighted to Triple-A Oklahoma City.