Marlins starter Caleb Smith gave up one run and three hits, striking out eight in six innings and five relievers pitched four scoreless innings.
Phillies left fielder Nick Williams made a perfect one-hop throw to the plate to prevent Miguel Rojas from scoring from second on Isaac Galloway's single to end the top of the ninth.
But Castro connected off Hector Neris (0-1) after Neil Walker lined a double off the left-field wall.
Tayron Guerrero (1-0) pitched a perfect ninth for the win. Sergio Romo finished for his third save in four tries.
Phillies ace Aaron Nola allowed one run and seven hits in 6 2/3 innings in his best start since the season opener. Nola exited with runners on first and third, but Adam Morgan retired pinch-hitter Galloway on a fly to center.
Miami has won four of six, yet is tied with Kansas City for the worst record in the majors at 8-17.
The Phillies have lost six of eight after an 11-6 start.
Sean Rodriguez hit a solo homer to give the Phillies a 1-0 lead in the second.
Martin Prado's RBI single in the third tied it at 1-1. Curtis Granderson was caught in a rundown trying to steal home to end the inning after Prado broke from first.
The Phillies had a chance to take the lead in the sixth when Andrew McCutchen led off with a double and moved to third on a fly out. But Bryce Harper fouled out and Rhys Hoskins flied out.
GRIND IT OUT
J.T. Realmuto fouled off nine straight 3-2 pitches in the first before Smith struck him out on the 16th pitch of the at-bat. Phillies fans gave Realmuto a partial standing ovation for his effort.
Marlins: OF/1B Garrett Cooper (left calf strain) was the designated hitter in an extended spring training game Thursday. He could start a rehab assignment on Friday, if he's cleared. ... RHP Riley Ferrell (right biceps tendinitis) threw a bullpen session Thursday.
Phillies: OF Roman Quinn was placed on the 10-day injury list with a right groin strain. OF Dylan Cozens was recalled from Triple-A Lehigh Valley to take his spot.
RHP Jose Urena (1-3, 5.74 ERA) starts for the Marlins and RHP Jerad Eickhoff (0-1, 3.60) goes for the Phillies. Urena is 3-4 in 11 starts vs. Philadelphia. Eickhoff is 2-3 in six starts vs. Miami.
More AP MLB: https://apnews.com/MLB and https://twitter.com/AP_Sports
Copyright 2019 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.