PHILADELPHIA — The Miami Marlins weren’t ready to give up — on Sunday’s game or their flickering playoff hopes.

J.T. Realmuto delivered a go-ahead infield single in the eighth, Christian Yelich belted a game-tying home run earlier in the inning and the Marlins rallied for a 5-4 victory over the Philadelphia Phillies to avoid a three-game sweep.

Derek Dietrich reached base three times and added an RBI for the Marlins, who came into the day six games off the pace in the NL wild-card race.

Reliever Mike Dunn improved to 4-1 and A.J. Ramos pitched a scoreless ninth for his 37th save.

“The guys know we’re not mathematically eliminated but we’re not in a very good spot,” Marlins manager Don Mattingly said. “We just have to keep fighting.”

With the Marlins trailing 4-2 in the eighth inning, Yelich hit a two-run homer off Hector Neris (4-4) that struck off the left-field foul pole. It was the 19th homer of the season for the center fielder.

Miami’s rally continued when Marcell Ozuna reached second as Cesar Hernandez and Tommy Joseph collided trying to catch a routine pop up. After moving to third on a groundout, Ozuna came home when Realmuto, who entered the game the previous inning, hit a sharp grounder down the line that third baseman Maikel Franco fielded cleanly but threw wide.

“This is how we play,” said Yelich, who leads the majors with 34 go-ahead RBIs. “We play hard all the time. If you play hard, you can live with the results.”

Freddy Galvis and Odubel Herrera both homered for the Phillies. Tommy Joseph added a pair of hits and an RBI.

Marlins starter Andrew Cashner, who lasted just two innings in his previous start, allowed three earned runs, five hits and three walks while striking out four through four innings. He labored through a rough 38-pitch first inning, giving up two runs and three hits and walking two.

Joseph, who hit his 20th home run of the season the previous night, put the Phillies in front with an RBI single in the first and Galvis followed with another RBI single two batters later.

Herrera added his 14th home run of the year to lead off the third, extending Philadelphia’s lead to 3-0. He’s now 12 for 23 in his last six games.

Galvis added another solo blast in the sixth, his 20th homer. He finished with three hits and also made a spectacular defensive play in the third.

“We let it slip away,” Phillies manager Pete Mackanin said. “We should have won that game.”

Marlins second baseman Dee Gordon extended his hitting streak to 10 games with an RBI single in the fifth, breaking up Alec Asher’s shutout bid. Dietrich followed with an RBI single to chase Asher before reliever Joely Rodriguez induced an inning-ending double play to preserve the Phillies’ 3-2 lead.

Making his third start of the season after serving an 80-game suspension for PEDs, Asher allowed six hits and two earned runs while striking out four and walking zero.

TIME TO LEAVE

Marlins third baseman Martin Prado was ejected after striking out to end the third, throwing his helmet against the ground in disgust and arguing with home plate umpire Dale Scott. Ichiro Suzuki replaced him.

Afterwards, Prado claimed that he was “mad at himself” and not at Scott, adding that it was just the second ejection of his 11-year career.

SURE-THING CLOSER

Ramos has converted 92.5 percent of his save opportunities this season (37 of 40), which is good for the third-best save percentage in the National League.

SIGN HIM UP

Some of the biggest cheers of the game came in the bottom of the second when a Phillies fan sitting along the right-field line caught a sharp line drive off the bat of Aaron Altherr — with one hand.

TRAINER’S ROOM

Marlins: Giancarlo Stanton was held out of the starting lineup for a second straight day after making his first start in the field on Friday following a three-week stint on the disabled list (left groin strain). He pinch hit in the seventh inning and struck out.

Phillies: Veteran slugger Ryan Howard missed his second straight game with knee soreness.

UP NEXT

Marlins: They open a 10-game homestand Monday vs. the Nationals, with Miami LHP Wei-Yin Chen (5-4, 4.99) facing Washington RHP A.J. Cole (1-2, 4.71).

Phillies: They begin a two-game home series vs. the White Sox on Tuesday with Philadelphia RHP Jake Thompson (2-5, 5.64) opposing Chicago RHP James Shields (5-17, 5.84).