NEW YORK -- Luis Castillo has spent most of August tutoring the rookie who took his spot in the lineup.
He gave Ruben Tejada quite a lesson as a late-inning replacement Tuesday night.
"That's big. Anytime the guys that aren't getting the opportunity to play anymore can contribute, it makes for a better team effort," manager Jerry Manuel said. "They hopefully feel a big part of what we're trying to accomplish. It was good to see Luis come up and get that game-winning hit."
Castillo looped a hit to the opposite field off Will Ohman (0-2) to score Ike Davis and help the Mets win for only the second time in 49 games when trailing after seven innings. New York also moved ahead of the Marlins and into third place in the NL East.
Manuel said Castillo's been "mentoring" Tejada since he was recalled Aug. 7 and seen most of his time at second base.
"Play your game," Castillo said of what he tells Tejada, "try and be ready every night."
Tejada was lifted for a pinch-hitter in the seventh, and Castillo came on as a defensive replacement in the eighth.
Davis led off the ninth with an infield single that sent second baseman Dan Uggla flipping to the turf trying to make a play. After Jeff Francoeur flied out to center, Josh Thole, newly designated as the Mets' starting catcher when Rod Barajas was sent to the Dodgers on Sunday, hit a broken-bat single. Pinch-hitter Mike Hessman struck out before Castillo completed the Mets' rally -- which was needed because they blew a two-run lead in the seventh.
"It stinks for us because we played a great game," Ohman said.
The Mets mobbed Castillo at first after Davis crossed the plate as right fielder Mike Stanton's throw sailed up the third base line and gave Florida its second straight loss after five wins in a row.
"I feel good. I feel good for the team, feel good for me," Castillo said. "It's hard because for the first time I'm not playing every day. I try to do the best I can."
Hisanori Takahashi (8-6) pitched a scoreless ninth for the win.
The Mets rallied to tie it with two outs in the eighth against Clay Hensley.
"The two-out hits, we have to find a way to close the inning," Marlins manager Edwin Rodriguez said. "That really hurt."
Angel Pagan hustled to stretch a grounder that hit off the mound and went into center field for a double, his career high-tying fourth hit. He scored on Carlos Beltran's single, the second run-scoring hit of the night for Beltran.
"That was a play, right there," Beltran said of Pagan. "The hit was good, but [the hustle] was a play."
The Mets took a 4-2 lead into the seventh, but Dickey gave up singles to Brett Hayes and pinch-hitter Emilio Bonafacio to start the inning. With two outs, Sanchez hit a line drive into the left-field seats on the first pitch he saw. His 15th home run gave Florida a 5-4 lead.
Dickey and Josh Johnson presented an enticing matchup of contrasting styles and backgrounds. A 35-year-old journeyman, Dickey was trying to match a career high with his ninth win. But in a game played on a damp, chilly night, he gave up a season-high 10 hits and allowed five earned runs. He struck out eight without walking a batter, using a knuckleball that dipped as slow as 53 mph in 17 mph winds.
Johnson, a 26-year-old hard-throwing All-Star, gave up six hits and four runs -- three earned -- in five innings. After posting a 1.35 ERA in July, he has given up three or more runs in three of his five August starts.
"Very inconsistent," Johnson said. "One good [game] and one bad is not what you want."
Leading 2-1 in the third inning, David Wright hit a sharp grounder with two outs that took a late hop that Helms couldn't handle. Pagan scored to tie it. Maybin, in his first game back in the big leagues after being demoted in mid-June, had Pagan's leadoff single to center field in the fifth skip under his glove for a two-base error.
Beltran singled home Pagan and Francoeur had the second of his two sacrifice fly balls to make it 4-2.
The Marlins went up 2-1 in the third with sacrifice fly balls from Sanchez and Uggla.
New York jumped ahead in the second when Wright led off with a triple and scored on Francoeur's sacrifice fly to right field.
Marlins RHP Ricky Nolasco (torn meniscus in right knee) will have a bullpen session Wednesday that should decide whether he is capable of starting again this season. Nolasco skipped his start Sunday, and played catch Monday and Tuesday. His next scheduled start is Sunday at Atlanta. ... The only other time the Mets won this season when trailing after seven innings was May 11 against Washington. They trailed 6-2 and won 8-6.