MILWAUKEE — This was one narrow escape for Kevin Gregg and the Chicago Cubs.

Gregg retired pinch hitter Scooter Gennett on a fly ball to the wall with a runner on, preserving the Cubs' 5-4 victory over the Milwaukee Brewers on Wednesday night.

Scott Feldman pitched six solid innings and Anthony Rizzo had a key two-run single for Chicago, which is tied with Milwaukee for last place in the NL Central. Luis Valbuena, Starlin Castro and Brian Bogusevic had two hits apiece.

Feldman (7-6) gave up three runs and six hits in his first victory this season against an NL Central opponent. He retired 11 of his last 14 batters.

"He'll manage an inning when things start to go a little haywire," Cubs manager Dale Sveum said. "He ends up giving up one run a lot of times instead of a crooked number. He manages the inning as well as anybody on this team."

But Feldman almost received a no-decision when Gregg struggled in the ninth. Juan Francisco started the inning with a homer and Rickie Weeks followed with a double. Weeks advanced to third on Logan Schafer's sacrifice and then was thrown at home when he tried to score on Yuniesky Betancourt's bouncer to third baseman Valbuena.

"Valbuena made that great stop and Welington (Castillo) hung onto it with Rickie Weeks (charging at him)," Feldman said. "You know that is like a linebacker coming at you there and he was able to hold onto the ball."

Gennett then hit a drive that Nate Schierholtz hauled in at the wall in right.

"The bottom line is if we win by one, great," said Gregg, who earned his 12th save in 12 chances. "You don't want to give up any runs but you want to win, that is the first and foremost thing as you go out there."

Feldman bounced back from a loss in his previous start at St. Louis. The 6-foot-7 right-hander could be an attractive target for a contender looking for pitching help ahead of next month's non-waiver trade deadline.

"There is nothing I can do about it," Feldman said. "I would love to stay on this team. I love the guys here. We are headed in the right direction. It is kind of out my control at this point."

Castro also scored a run in his return to the lineup after sitting out Tuesday's game, snapping his streak of 269 consecutive games played. Castro has been battling a terrible June slump, hitting .146 this month with no home runs and three RBIs.

Ryan Sweeney homered in the third and Valbuena scored on Rizzo's single in the fourth that was set up by an error on second baseman Weeks.

"That was huge." Sveum said. "We had numerous chances to break that game wide open and luckily we got one hit out of all that stuff to separate us a little bit."

Milwaukee right-hander Yovani Gallardo (6-7) struggled with his command and was charged with five runs, three earned, and eight hits in four innings. He walked four and struck out five.

"Tough outing for him," Brewers manager Ron Roenicke said. "His command was just off. He made some good pitches, but he didn't have the consistency. Usually even when he gets high pitch counts and he's a little off on his command, he still keeps us in the ballgame better than he did tonight. But we also didn't play very good behind him."

Gallardo came into the game with a 21-inning streak of not allowing an earned run. After escaping a first-inning jam, his streak was snapped when he gave up two runs in the second.

"The ball was up," Gallardo said. "I was leaving it up in the zone and that's what led to the big hits they got. I felt good but that was the main thing, leaving the ball up in the zone."

Brewers slugger Aramis Ramirez got his 2,000th career hit with a solo home run in the second inning. Ramirez, who ranks 15th in hits among active players, added an RBI single in the sixth inning.

NOTES: The Brewers optioned Gennett to Triple-A Nashville after the game and selected the contract of first baseman Sean Halton from their top minor league club. ... Brewers OF Carlos Gomez missed his second straight game with a left shoulder sprain. Gomez was injured Sunday when he crashed into the outfield wall while making a diving catch. ... Bogusevic made his Cubs debut after his contract was selected Tuesday from Triple-A Iowa after reliever Carlos Marmol was designated for assignment. ... Weeks extended his hitting streak to 10 games with a single in the fourth. ... The Cubs snapped a nine-game losing streak at Miller Park.