MIAMI -- The Florida Marlins had seen this before, usually from the other side.
"It was great," Sanchez said. "We are usually on the other side of it. Now we're on this side doing it to the other team."
Florida snapped a three-game losing streak, while Philadelphia's five-game win streak ended.
Bastardo entered to begin the eighth with Philadelphia holding 4-3 lead. He walked the first two batters -- Omar Infante and Logan Morrison -- before Herndon came on. Jose Lopez sacrificed to advance the runners and Sanchez followed with his 18th homer.
"We've been on the other end of those bases on balls," Marlins manager Jack McKeon said. "Finally tonight, we got a couple in our favor. That set the inning up because we had to tie it before we could win it."
Cameron then hit his second home run and pinch hitter Greg Dobbs added a two-out solo blast off Herndon in the five-run eighth.
"That's the situation (a reliever) wants, they just hit the mistakes ... kind of hard," Herndon said.
Edward Mujica (9-5) pitched one scoreless inning for the win.
Florida starter Ricky Nolasco allowed four runs and seven hits in six innings. He walked one and struck out three.
Stanton left the game with right hamstring tightness after he grounded out slowly to end the fifth inning. He was listed as day to day.
Stanton's 32nd home run in the third tied the score at 3. Stanton sent Hamels' 1-1 pitch into the second deck in left field for a two-out, solo shot to pull Florida to 3-2. Cameron hit a 1-1 pitch to left-center following a leadoff walk to Sanchez.
Philadelphia took a 3-0 lead in the second on Valdez's two-run triple and Hamels' run-scoring infield single.
The game started after a 1-hour, 38-minute rain delay. The first two innings were played in a steady, heavy drizzle. ... Stanton began the day tied for second in the league in home runs. ... The Marlins announced SS Hanley Ramirez will not play again this season and is expected to undergo left shoulder surgery. Ramirez should be ready for opening day next season, when the team opens a new ballpark in Miami. ... Phillies ace Roy Halladay (16-5) pitches the series finale Sunday against Florida's Anibal Sanchez (7-7).