NEW YORK — The playoff chase finally caught up with the New York Yankees on Sunday.

On a day when Chase Headley homered and old friend Chase Whitley returned from Tommy John surgery against his former team, the Yankees lost ground in their postseason pursuit.

Corey Dickerson hit a two-run homer, Matt Andriese won for the first time since June and the Tampa Bay Rays slowed New York’s late charge by stopping its seven-game winning streak with a 4-2 victory.

“We didn’t give them the game,” Headley said. “They just beat us today.”

Logan Forsythe and Brad Miller also went deep for the last-place Rays, who had dropped three straight and six of seven. But they prevented a four-game sweep behind Andriese (7-7) and Whitley, who tossed two innings of relief in his Tampa Bay debut.

Headley hit his 14th home run and Brett Gardner had an RBI single among his three hits for the Yankees, who fell four games behind first-place Boston in the AL East and two back of Baltimore for the league’s second wild card.

“Every loss at this point stings just because of how few games are left,” manager Joe Girardi said.

Whitley struck out his first batter, rookie slugger Gary Sanchez, and gave up an unearned run. But he retired Jacoby Ellsbury on an easy fly with two runners in scoring position to protect a 4-2 lead in the seventh.

It was the right-hander’s first major league outing since he started for the Yankees at Tampa Bay on May 14 last year.

“That was pretty cool. Just unbelievable,” Whitley said. “I’ve still got a lot of friends over there. So it was just really, really fun.”

Whitley even took a photo with Yankees head physician Dr. Christopher Ahmad, who performed the pitcher’s elbow operation.

“It was a long road for him, so we’re all happy for him,” Girardi said. “I wish he would have gave up a few runs today, but the rest of the way I hope he does well.”

Brad Boxberger worked a perfect eighth against the 3-4-5 hitters in New York’s lineup, and Alex Colome got three quick outs for his 32nd save in 34 attempts.

“We could not have scripted that any better,” Rays manager Kevin Cash said. “A good bounce-back win for the guys.”

Andriese allowed one run and six hits over five innings, snapping a seven-game losing streak with his first win since June 15 against Seattle. The right-hander won his first six decisions this season but was 0-7 with a 6.11 ERA in his past 15 outings, eight of them starts.

Dickerson and Nick Franklin both had three hits. Miller’s homer ended an 0-for-17 slump.

Rookie right-hander Luis Cessa (4-1) gave up all three Rays homers in 5 2/3 innings. New York had won his first four starts.

The Yankees were looking for their first eight-game winning streak since a 10-game run in June 2012. They lost for only the fifth time in 18 games since Aug. 23.

“Of course it would have been great to keep it going, but we’ve just got to start a new one tomorrow,” Headley said.

9/11 CEREMONIES

Before the game, Girardi and Yankees reliever Dellin Betances placed a wreath at the 9/11 Monument in Monument Park to honor those killed in the terrorist attacks 15 years ago. Following a moment of silence, FDNY firefighter Frank Pizzaro sang the national anthem while other firefighters unfurled a large American flag in center field — the same flag displayed when President George W. Bush threw out the first pitch before Game 3 of the 2001 World Series at the original Yankee Stadium. After their roll call of Yankees starters, the Bleacher Creatures briefly chanted “USA! USA!”

TRAINER’S ROOM

Rays: 1B Logan Morrison strained his left wrist when he hit a foul line drive in the fourth and left the game. “Felt it pop on that one swing,” he said. “More than likely, I’m probably done for the year.” Morrison returned from a strained right forearm on Aug. 15. … Miller was back at shortstop for the first time since Thursday, when he was hit by a pitch on the right elbow. He was the DH on Friday and played first base Saturday.

Yankees: 2B Starlin Castro received a rare rest with New York in the middle of playing 17 consecutive days. Rookie INF Ronald Torreyes started at second. Castro pinch-hit for Torreyes in the seventh and reached on a two-out error.

UP NEXT

Rays: RHP Jake Odorizzi (9-6, 3.86 ERA) pitches Monday night in the opener of a three-game series at Toronto, where the Rays are averaging 7.5 runs in six games this year. They are 9-7 against the Blue Jays overall and need one win to clinch the season series for the eighth time in nine years.

Yankees: RHP Bryan Mitchell (1-0) makes his second start this season Monday night when the NL West-leading Los Angeles Dodgers visit Yankee Stadium in the renewal of an old October rivalry. Mitchell missed most of the year with a toe injury and earned his first major league win last Wednesday with five scoreless innings against Toronto. Rookie RHP Jose De Leon (1-0) pitches for the Dodgers. He won his big league debut last Sunday against San Diego, striking out nine and walking none over six innings.