Trailing 3-2 entering the frame, Omar Infante began with an infield single off Cole Hamels (8-2) and Ramirez followed by lofting a 2-2 pitch into the left-field stands for the decisive blow.
The Marlins third-baseman also went deep in the fourth for the Marlins, who won for the fourth time in five games and evened their three-game-series at Citizens Bank Park.
Ricky Nolasco (6-3) worked around four runs on eight hits to secure the victory in a seven-inning start, striking out five and walking two.
Steve Cishek tossed a scoreless eighth and Heath Bell picked up his 11th save for Miami.
Hamels, meanwhile, absorbed his first loss since April 9 -- a span of 10 starts. The left-hander surrendered five runs on seven hits in a 6 2/3-inning stint, recording eight strikeouts.
Hunter Pence drove in two runs with a solo homer and sacrifice fly for the Phillies.
Swim Week Miami Beach 2014 [PHOTOS]