PHOENIX -- With his slumping team down three runs in the sixth inning, Arizona manager Bob Melvin started making notes for a postgame pep talk.
"It didn't look good early on," Melvin said. "As a matter of fact, I was drawing up a little speech for after the game."
The Diamondbacks spared him the trouble, rallying for a 4-3 victory on Adam Dunn's winning double in the ninth.
Arizona won for only the third time in 10 games and remained 1½ games in front of the Los Angeles Dodgers.
Arizona won in its last at-bat for the third time this season. But this victory seemed bigger because the Diamondbacks hope it will provide momentum heading into a pivotal three-game series beginning Friday night in Los Angeles.
"We've done it before, but it's difficult to do this time of year," said Conor Jackson, who singled in the tying run.
For the second time in three games, Arizona rallied against the Cardinals' tattered bullpen, whose 29 losses are most in the majors.
One out later, Jackson singled through the drawn-in infield to tie it and give Perez his second blown save in eight chances.
Dunn followed with a shot to the right field corner to score Jackson ahead of the throw. Dunn said he went to the plate looking for a fastball, and Perez delivered it.
"The last one, to Dunn, was probably the worst pitch of the time," Perez said. "It was right over the middle.
"He put a good swing on it," Perez said. "He's a good hitter."
After sliding across the plate, Jackson bounced up and dashed out to second base, where Dunn was being mobbed by his teammates.
"I expected a little more rough-house," Dunn said. "That was exciting, man."
St. Louis manager Tony LaRussa didn't fault Perez.
"Good hitting against him," LaRussa said. "Sometimes you've got to give the other side credit."
Chad Qualls (4-8) pitched the ninth to earn the victory.
It was a crushing loss for St. Louis, which also blew an early four-run lead in an 8-6 loss in the series opener. The Cardinals won the middle game of the series.
St. Louis went 1-for-10 with runners in scoring position on Wednesday, and 3-for-21 in the series.
The Cardinals wrapped up a 1-5 road trip and missed a chance to shave a game off Milwaukee's lead in the NL wild-card race. The Cardinals trail the Brewers by 5½ games.
"It's been a tough road trip for us," Perez said. "We had a chance to maybe salvage a little bit with a win today and pick up a game on Milwaukee, but it just didn't happen for us."
The Diamondbacks mustered nothing against Cardinals starter Kyle Lohse, who threw six shutout innings but was denied his first victory since Aug. 1.
Arizona trailed 3-0 in the seventh when pinch-hitter Augie Ojeda led off with a double against reliever Kyle McClellan. One out and two walks later, Ojeda scored on Jackson's fielder's choice grounder to cut St. Louis' lead to 3-1.
St. Louis took a 2-0 lead in the third when Cesar Izturis bunted for a single and Troy Glaus followed by hitting an 0-1 delivery from Doug Davis into the left field seats -- the 300th homer of Glaus' career.
In the sixth, Felipe Lopez reached on an infield single and took second on third baseman Mark Reynolds' throwing error, his 27th error. Lohse followed with a double down the right field line to give the Cardinals a 3-0 lead.
At that point, Melvin started thinking about giving his slumping team a speech after the game. He chuckled when asked what he planned to say.
"I've got it written down on my lineup card," Melvin said. "I'll save it."
- St. Louis pitching coach Dave Duncan was ejected by home plate umpire Dale Scott during a mound visit in the seventh.
- Reynolds tied a career high with four strikeouts, giving him 178 for the season. Only Philadelphia's Ryan Howard has more.
- Arizona LF Eric Byrnes, out since July 1, said he was encouraged by an MRI taken Tuesday on his injured hamstring. "It's healthy and it's healing quickly," said Byrnes, who isn't expected to return until spring training.
- St. Louis 1B Albert Pujols has hit 30 homers in his first eight seasons. According to Elias Sports Bureau, that's double the next-longest streak of 30-homer seasons to start a career. Mark McGwire did it his first four seasons, from 1987-90.