Pete Alonso drew a bases-loaded walk with two outs, capping a chaotic ninth inning and lifting the Mets to a 5-4 victory over the Philadelphia Phillies on Friday night at Citi Field.

The Mets pulled into a tie with Philadelphia, four and a half games behind the Chicago Cubs for the second National League wild-card spot. The Cubs were at Milwaukee.

The Mets took a 4-2 lead in the bottom of the eighth on run-scoring singles by Alonso and Wilson Ramos. However, the embattled closer Edwin Diaz (2-7), pitching for the first time since he gave up a game-ending three-run homer to Kurt Suzuki as the Washington Nationals completed a seven-run ninth-inning comeback on Tuesday night, gave up a one-out, two-run homer to J.T. Realmuto in the top of the ninth.

But the Mets bailed out Diaz in the bottom of the inning, as Mike Morin (1-2) retired the first two batters he faced before Juan Lagares singled to left-center. Lagares went to third when J.D. Davis, pinch-hitting for Diaz, got ahead 3-0 before singling to left on a full-count pitch.