BOSTON — Bases loaded. Two outs. Already trailing by two runs in the fifth inning, New York Yankees Manager Joe Girardi decided he couldn’t wait any longer.

Out went starter Bartolo Colon. In came Boone Logan, the only left-hander in the bullpen, to face Adrian Gonzalez.

“That’s a big situation,” Logan said Friday night after striking out Gonzalez, who leads the majors with a .356 batting average and 91 RBI. “You can’t think of it like, ‘It’s Adrian Gonzalez.’ That’s where you can get into trouble.”

Five New York relievers combined to throw 41/3 shutout innings after Colon couldn’t make it out of the fifth, and the Yankees scored three in the top of the sixth to win 3-2 and take over the lead in the AL East.

It was the eighth straight win for the Yankees, who had beaten Boston just once in nine previous meetings this season.

“They’re human,” right fielder Nick Swisher said. “They can be beat. It was a big lift for us.”

Swisher had two hits, including an RBI double off Jon Lester (11-5) to give the Yankees a 3-2 lead. Mariano Rivera pitched the ninth for his 29th save, striking out Josh Reddick for the final out as the Yankees took over the top spot in the division for the first time since July 6.

The Red Sox led 2-0 after four and loaded the bases against Colon in the fifth. With Gonzalez up, Girardi brought in Logan (3-2).

“Bases loaded, there’s no room for error on Boone’s part. That’s a huge statement,” Girardi said. “It’s a big win, to be able to come from behind when Lester was dealing; to be able to scratch three runs across by some great at-bats. That was a big win for us.”

Lester, who had won five consecutive starts against the Yankees, allowed three runs on five hits and four walks, striking out seven in six-plus innings.

David Ortiz homered and Reddick had a pair of hits for Boston, which had been a season-high three games in front of New York on July 24.

They still led by 2½ on the morning of July 30 before the Yankees swept the Orioles in a doubleheader to start their winning streak. But the teams came into the three-game series at Fenway Park tied for the best record in the AL, both 68-42.

“It’s probably going to be close,” Red Sox outfielder Carl Crawford said. “A photo finish.”

It was a pleasant 71 degrees for the first pitch, and the crowd of 38,006 was the 688th consecutive sellout at Fenway Park.

The fans saw Lester outlast Colon, but only long enough to run into trouble himself in the sixth.

Lester walked No. 9 hitter Eduardo Nunez, and gave up back-to-back singles to Derek Jeter and Curtis Granderson to score one run before Mark Teixeira walked to load the bases. The Yankees tied it on Robinson Cano’s double play, then took a 3-2 lead on Swisher’s double down the third-base line.

Colon allowed two runs on six hits and a walk. He gave up Jacoby Ellsbury’s RBI double in the third and Ortiz’s solo homer in the fourth, then loaded the bases in the fifth before Logan (3-2) struck out Gonzalez on a checked swing.

“I just wasn’t able to hold back,” Gonzalez said. “(The bullpen) did a good job. You’ve got to tip your cap to them.”

Logan pitched one inning, giving up one hit and striking out one for the Yankees.