ST. LOUIS -- Carlos Martinez was far from dominant Friday night, but he gave the St. Louis Cardinals what they needed the most: a victory.

Martinez snapped a string of sub-par performances from Cardinals' starters by pitching seven innings to get the 4-3 win over the Milwaukee Brewers.

In raising his record to 14-7, Martinez needed 102 pitches to get through his outing, allowing nine hits, walking one and hitting two batters. He did not record a 1-2-3 frame until his final inning of work.

Martinez even started the offensive rally which saw the Cardinals score all of their runs in the span of four batters in the third inning.

Trailing 3-0 with two outs in the inning, Martinez singled before Matt Carpenter homered to cut the lead to 3-2. Kolten Wong drew a walk, followed by another two-run homer by Stephen Piscotty which put the Cardinals ahead to stay.

"I felt a little but amped up because I wanted to win the game and I was able to tone it down," Martinez said through an interpreter. "It's go time. I prepare all offseason for September. This is when you really have to test yourself and give it your all."

Kevin Siegrist worked a 1-2-3 eighth to turn the one-run lead over to Seung Hwan Oh in the ninth, who worked around a leadoff work for his 17th save.

The win allowed the Cardinals to remain a half-game behind the New York Mets for the second National League Wild Card spot. The Mets beat the Atlanta Braves 6-4 on Friday night, rallying from a 4-0 deficit.

The Brewers had built their early lead on a run in the first, without the benefit of a hit, and a two-run homer by Scooter Gennett in the third. The three runs matched the total number of runs Martinez had allowed in his last six starts against the Brewers, a span of 34 innings, dating back to the start of last season.

Gennett's homer, his 11th of the season, was the first Martinez had allowed to any current hitter on the Brewers.

"He was a little jumpy at the beginning," Cardinals manager Mike Matheny said of Martinez. "We had to have him. Carlos has been so consistent that you begin to anticipate he's going to have that kind of outing."

Martinez is now 4-0 in his last five starts, allowing eight runs in 34 innings. The Cardinals had not had a starter go past the fifth inning in their previous four games.

"He always competes," Piscotty said of Martinez. "He gave up a few runs but we knew with our offense we could come back. I was just glad he was able to keep us right there."

The Cardinals had only five hits off Milwaukee starter Jimmy Nelson, with three of them coming in the four-batter sequence in the third.

"We didn't have a lot of hits but we made the most out of them," Matheny said.

The homer by Carpenter was his 19th of the season and it was the 21st for Piscotty, increasing the Cardinals' total for the season to 204.

"Two of the guys who can really hurt him hurt him," Brewers manager Craig Counsell said of Nelson. "When you make mistakes to those guys that's what's going to happen."

With the loss, Nelson fell to 0-7 in his career against the Cardinals and 7-14 on the season. It was only the second loss for the Brewers in their last nine games.

"They did their job right there," Nelson said. "That's definitely frustrating to give up the lead.

"It's just a crazy game and stuff happens. There's never really just one answer in this game to anything, whether it's hitting or pitching."

With 22 games to go in the season, it's still a little early to be scoreboard watching, but Piscotty knows exactly where the Cardinals are in the Wild Card race.

"We know the importance of each game," he said. "There's no ground to give up now. We've had the mindset of one game at a time all year, and I think that's going to pay off."

NOTES: Cardinals SS Aledmys Diaz played the second game of his rehab assignment for Double-A Springfield on Friday night, going 2-for-4 with a double. The team is hopeful he can be back in the lineup as early as Sunday for the first time since breaking his right thumb when he was hit by a pitch on July 31. ... RHP Trevor Rosenthal, also on the DL for the Cardinals, is scheduled to throw live batting practice on Saturday, manager Mike Matheny said. ... The Brewers, who went to a six-man rotation when RHP Junior Guerra came off the DL, intend to keep using six starters for the time being. In their first six games since making the change the Brewers were 5-1 and the starters allowed just eight earned runs in 34 innings, a 2.12 ERA. ... The Cardinals' game against the Cubs on Sept. 25 has been picked as the Sunday night game by ESPN, with a starting time of7:08 p.m.