BOSTON -AP- Hanley Ramirez hit a three-run homer with two outs in the ninth inning to finish off a five-run rally as the Boston Red Sox came back to beat the New York Yankees 7-5 on Thursday night and open a two-game lead in the AL East.New York took a four-run lead after three innings and still led 5-1 beforeDavid Ortiz hit a solo shot in the eighth. It was the 537th home run of his career, passing Yankees Hall of Famer Mickey Mantle for 17th on baseball's all-time list. Ortiz also had an RBI single off Dellin Betances (3-6) in the ninth.There were still two on and two outs when Ramirez worked a 3-1 count and then sent the next pitch to straightaway center field. The victory in the opener of a four-game series against its archrival snapped Boston's two-game skid.Starlin Castro had four hits and new addition Billy Butler drove in a pair of runs in his debut for the Yankees, who have lost four of five.Joe Kelly (3-0) pitched into and out of a bases-loaded, one-out jam in the top of the ninth, striking out Chase Headley and then getting Mason Williams on a line drive back to the mound.The Red Sox moved two games ahead of Baltimore, which lost 7-6 to Tampa Bay. Instead of improving to three games back in the division, the Yankees face a five-game deficit.Ortiz had three RBI and three hits -- two for extra bases to give him 1,186 in his career. That tied Ortiz with Frank Robinson for 11th on the all-time list for extra-base hits.Yankees manager Joe Girardi tried to avoid using Betances in the ninth, starting with Tommy Layne for one out and then turning to Blake Parker, who nipped pinch-hitter Chris Young on the top of the helmet with a pitch.Betances, the loser in Wednesday's game against the Dodgers, walkedDustin Pedroia but got the second out when Young tried to score from third on a short grounder and was cut down at home.With the crowd chanting "Papi! Papi!", Ortiz singled to make it 5-3.Mookie Betts singled to make it 5-4, and Gary Sanchez's passed ball put runners on second and third before Ramirez ended it.It was the sixth game-ending hit of Ramirez's career and his first for Boston, earning him an ice-water drenching in front of the dugout as the crowd celebrated. It was the first walk-off homer for the Red Sox sinceMike Napoli in 2014.