A night earlier, the Phillies scored four runs in the ninth off Francisco Rodriguez to beat Milwaukee by the same score.
The Brewers wasted a sensational outing by Zack Greinke, who outpitched and homered off Cliff Lee.
Greinke allowed one run and three hits, striking out five. He retired the last 14 batters he faced, but left after seven innings with the Brewers up 6-1 only to see them lose their season-worst fifth straight.
Pinch-hitter Eric Kratz got the Phillies started in the eighth with a two-run homer off Manny Parra to cut it to 6-3. Shane Victorino, Chase Utley and Ryan Howard worked consecutive two-out walks to load the bases.
Kameron Loe (4-4) entered to face Ruiz, who ripped a 1-2 pitch to the gap in left-center to clear the bases. Pence followed with a looping RBI single to right.
The last-place Phillies suddenly seem like the team that has won five straight NL East titles. They have won three straight, including consecutive walkoff wins.
Still, the Phillies entered the night 10 games behind in the wild-card race.
Michael Schwimer (1-1) pitched a scoreless eighth to earn the win. Jonathan Papelbon finished for his 22nd save in 25 tries.
With several scouts watching, Greinke showed he can certainly help a contender down the stretch if the Brewers decide to deal him before the July 31 non-waiver trade deadline rather than risk losing him in free agency after the season.
The 2009 AL Cy Young Award winner was scratched from his last start because of fatigue after pitching three straight games around the All-Star break. But he proved he doesn't have a tired arm, consistently throwing his fastball in the low 90s and reaching a high of 94 mph.
Ryan Braun, Carlos Gomez and Aramis Ramirez also hit homers off Lee.
Lee gave up six runs and 12 hits in seven-plus innings. It's unfathomable that Lee, who is earning $21.5 million this season, has one win in 17 starts.
It hasn't been entirely Lee's fault, though. The 2008 AL Cy Young Award winner hasn't received much run support and couldn't even win a game in which he threw 10 scoreless innings in April. The Phillies have scored three runs or fewer in 12 of his 17 starts.
The offense bailed him out this time.
Braun ripped his NL-leading 27th homer over the ivy-covered brick wall in center to give the Brewers a 2-1 lead in the sixth.
Greinke connected in the seventh, driving his third career homer out to left.
Gomez hit one way out off the facade of the second deck in left in the eighth. Ramirez ended Lee's night with a two-run shot later in the inning.
Lee struggled at the start, allowing four hits and a run in the first. He retired 10 straight batters before Martin Maldonado started the fifth with an infield single. The Brewers loaded the bases with one out, but Lee picked Maldonado off third base and struck out Gomez to end the inning.
Notes: Four homers allowed by Lee tied a career-high. Orioles did it against him on Aug. 21, 2010. The 12 hits also tied a career-high. ... The Phillies are 12-4 in their last 16 games vs. Milwaukee. ... The NL Central is the only division the Phillies have a winning record against this season. They are 14-7. ... Brewers RHP Shaun Marcum threw a bullpen session. He hasn't pitched since June 14 because of right elbow tightness. ... Rickie Weeks is 1-for-17 on Milwaukee's road trip. ... The Phillies had their 269th straight sellout, including postseason play. ... Vance Worley (5-6, 3.82) pitches for the Phillies against Marco Estrada (0-4, 4.10) when the teams finish their series Wednesday afternoon.