NEW YORK — The Yankees' four-game set against the AL-leading Rangers started Monday with what Chase Headley called maybe the toughest loss of his career.

On Wednesday, Texas was two outs from winning the first three games. And yet the series ended Thursday with New York somehow earning a split when Headley scampered home on a passed ball for his team's second straight victory in the bottom of the ninth.

Headley just beat the throw with two outs for a 2-1 win. Four Yankees pitchers combined to strike out 16 Rangers — 12 by starter Michael Pineda.

Didi Gregorius, who had the winning home run hours earlier when the Yankees rallied from a four-run ninth-inning deficit Wednesday night, hit a solo shot in the fifth to tie the score. Shin-Soo Choo had homered to lead off the game.

Reliever Tony Barnette (5-3) walked Headley to lead off the bottom of the ninth. With runners on second and third and two outs, Robinson Chirinos dropped a fastball from Barnette.

Headley had been concerned that Chirinos might try to throw behind him, and when he saw the pitch wasn't low, he started to take a half-step back before he saw the ball squirt away.

"Once it got by, though, there's no going back," he said. "You just hope that you make it."

Chirinos said he knew which pitch was coming and its location.

"It's my fault," he said.

Barnette blamed himself for the leadoff walk.

"It's unacceptable," he said. "You walk a guy to start an inning, most of the time he's going to score."

Aroldis Chapman (2-0) worked around Adrian Beltre's leadoff single in the top of the ninth to earn the win.

In what manager Joe Girardi called his best outing of the season, Pineda was sharp after a shaky first inning, when Choo crushed the fourth pitch of the game for his 18th career leadoff homer.

Pineda struck out Rougned Odor with two runners on to prevent further damage, and he didn't allow a hit the rest of the way — reaching what is a magical six-inning mark for Yankees starters because of their trio of hard-throwing relievers.

Dellin Betances, Andrew Miller and Chapman each pitched a scoreless inning.

After a rough start to the season, Pineda had a 2.75 ERA in June, though he has just one win to show for it because of a lack of run support.

Like Pineda, Rangers starter A.J. Griffin cruised after labouring through the first. Both pitchers struck out the side in the second, and overall nine straight batters struck out from the bottom of the first through the top of the third.

Griffin retired 12 in a row before Gregorius' tying homer. Griffin departed after throwing 88 pitches in five innings. He allowed the one run and two hits with two walks and eight strikeouts.

After Headley's walk in the ninth, Gregorius came up again with a chance to win it — but this time he sacrificed, bunting Headley to second with one out. Aaron Hicks then walked.

Starlin Castro, who hit a walk-off home run of his own eight days earlier, grounded out to first to put the runners on second and third with two outs.

MOMENTUM SWING

On Monday, the Yankees blew a ninth-inning lead after a 3 1/2-hour rain delay, and the game didn't end until 2:44 a.m. But now they start a 10-game road trip back at .500.

"Hopefully this is what really got us going," Girardi said.

TRAINER'S ROOM

Yankees: Carlos Beltran, who didn't start for the second straight game because of right hamstring tightness, can bat, but he can't run. He pinch-hit for No. 9 hitter Ronald Torreyes in the seventh, and after he walked, Castro, who didn't start with a day game after a night game, pinch-ran for him. Girardi is concerned about Beltran's ability to play the field during this weekend's interleague series at San Diego and doesn't expect him to start Friday. ... C Brian McCann played a day game after a night game even though tendinitis in his knee flared up Wednesday.

UP NEXT

Rangers: Texas continues its 10-game road trip with a three-game series at Minnesota, with LHP Martin Perez (7-4) seeking his seventh straight victory when he faces RHP Ervin Santana (2-7) on Friday.

Yankees: New York opens a 10-game road trip with an interleague series at San Diego, when RHP Nathan Eovaldi (6-5) looks for his first win in more than a month in a matchup against RHP Colin Rea (4-3).