Ramirez hit two homers, both off Hamels, and drove in three runs to help the Marlins beat the Philadelphia Phillies 5-4 on Saturday.
Hamels (8-2) gave up a season-high five runs and seven hits in 6 2/3 innings. He is 0-2 in two starts against Miami and 8-0 in nine starts against everyone else.
"We faced one of the best pitches in the game right now and we handled him pretty well," Marlins manager Ozzie Guillen said.
Ricky Nolasco (6-3) gave up four runs and eight hits in seven-plus innings, helping the Marlins to their sixth win in eight games while denying Hamels his ninth victory.
"I told the coaching staff Nolasco better bring his `A' game today because we know Cole is going to throw well," Guillen said. "After the first couple of innings, he pitched well. He kept us in the game and gave us a chance to win."
Nolasco improved to 6-1 in seven career starts at Philadelphia.
"Even just watching games here, it's so exciting," Nolasco said. "The fans sell it out. If you don't enjoy pitching here, whether you're a visitor or if this is your home team, then something is wrong with you."
Jose Reyes had an RBI double to extend his hitting streak to 13 games.
Hunter Pence homered for the second straight game for the Phillies, who lost for the third time in 10 games.
Ramirez homered in his second straight at-bat in the sixth, a two-run shot to left, to give Miami a 4-3 lead. After Omar Infante led off the inning with an infield single, Ramirez drove Hamels' 2-2 changeup into the seats in left for his 12th career multihomer game.
"Hanley is a good hitter -- one of the best hitters in the game," Guillen said. "He's got some credentials. Hanley is good against anybody; there's no doubt in anybody's mind that kid can hit."
Ramirez said he didn't do anything different against Hamels.
"You don't change your approach," he said. "You just go out there looking for a good pitch to hit and it's what we've been doing."
Reyes increased the lead to two runs with a two-out RBI double to left that put Miami up 5-3 and spelled the end for Hamels.
"They are a good team, but I think that both games that I pitched against them, it's a matter of making too many mistakes," Hamels said.
It was the left-hander's first loss since his first start, a 6-2 defeat against Miami on April 9. Hamels was attempting to become the first nine-game winner in the majors.
"I felt like he got a little bit out of whack the last two innings he pitched," Phillies manager Charlie Manuel said.
The Phillies got a run back in the eighth. Jimmy Rollins led off with a triple to right and Juan Pierre walked to put runners on first and third. Guillen lifted Nolasco for Steve Cishek. Pence hit a sacrifice fly off Cishek, scoring Rollins, but the right-hander sandwiched strikeouts of Carlos Ruiz and Ty Wigginton around a walk to Shane Victorino to preserve Miami's lead.
Pierre scored in the first on Ruiz's groundout to give the Phillies a 1-0 lead. Ruiz entered leading the majors in batting at .381.
Rollins' RBI infield single in the second and Pence's leadoff homer to right in the third put Philadelphia ahead 3-0.
The Marlins got a pair of runs back in the fourth on Ramirez's leadoff homer to left-center and Justin Ruggiano's RBI double to right. Umpires reviewed Ramirez's shot to see if a fan interfered with the ball, but confirmed it was a homer after a short delay.
"Hanley is a great hitter," Nolasco said. "He started off slow but he's been coming around. He's seen Cole Hamels a lot and maybe that had something to do with it, but he put together two big at-bats that were pretty much the whole game for us."
Ramirez is batting .444 (20 for 45) in his last 12 games.
"We've been playing pretty good baseball, so we're going to try to continue to do that," Ramirez said. "Everybody is having fun and everybody is together. That's what you can call a team."
Jose Contreras' season is likely over after an MRI revealed the Phillies reliever has a torn UCL and flexor pronator tendon in his right elbow. The 40-year-old Contreras had right elbow surgery last year. He reinjured the elbow in the seventh inning Friday night while facing Infante. The crowd of 45,509 was the Phillies' 230th straight regular-season sellout and 246th counting postseason play. ... Philadelphia fell to 0-20 when trailing after seven innings. ... Marlins RHP Carlos Zambrano (3-3, 3.00 ERA) is scheduled to face Phillies RHP Joe Blanton (4-5, 5.05) on Sunday in the finale of the three-game set.