BOSTON (AP) — Mitch Moreland put some recent struggles behind him with a clutch hit in the ninth inning. Helped the major league-leading Boston Red Sox do the same with their weekend woes, too.

Moreland singled in the winning run with two outs in the ninth and the Red Sox beat the Houston Astros 6-5 on Sunday night to avoid a three-game sweep in a matchup of AL division leaders.

“Yeah, I’ll take those every time if I could,” he said, breaking into a grin. “It was at the right time to do it, I guess.”

Coming into the game in a 2-for-26 slump, the first baseman had a pinch-hit single in the eighth before his walkoff bloop.

“Yeah, he gets jammed and he gets a single,” Red Sox manager Alex Cora said, adding “we need this guy to get going offensively. We know how good he is.”

With runners on first and second, Moreland sent his opposite-single to left against Hector Rondon (2-3), and pinch-runner Tzu-Wei Lin raced home easily. Left fielder Marwin Gonzalez tried to barehand the ball for a quick throw but lost his grip and let it dribble toward the Green Monster.

J.D. Martinez hit his 40th homer and drove in four for Boston, which snapped Houston’s seven-game winning streak even after the Astros rallied with a four-run sixth inning to tie the game at 5.

The East-leading Red Sox sliced their magic number to 11 for their third straight division title. Boston leads the second-place Yankees by 8 ½ games after they lost at Seattle.

Jose Altuve and Gonzalez each hit a solo homer for the West-leading Astros, who saw their lead over Oakland cut to 2 1/2 games. The Athletics beat Texas earlier in the day.

“This was a really good game, really good series,” Houston manager A.J. Hinch said. “We win this series, which is one of the goals coming into this.”

Craig Kimbrel (5-1) pitched a perfect ninth to preserve a tie.

Trailing 5-1 in the sixth, the Astros rallied against starter Rick Porcello and Boston’s recently struggling bullpen. Altuve led off with a shot over the Monster, Tyler White had a two-run double off Heath Hembree, and Josh Reddick hit a tying double off Brian Johnson.

Boston had built its lead on Martinez’s three-run shot an inning earlier. Martinez’s sacrifice fly had pushed Boston ahead 2-1 in the third.

“Just one mistake pitch to J.D.,” Houston starter Dallas Keuchel said after giving up five runs in six innings. “I knew he was swinging. We have a long history together.”

The Red Sox loaded the bases with one out in the eighth inning, but reliever Tony Sipp entered and struck out pinch-hitters Blake Swihart and Christian Vazquez.

Porcello gave up four runs in 5 2/3 innings.

LONG WAIT

There was a 3-minute, 50-second review of a key play at the plate where Red Sox catcher Sandy Leon jumped for a throw from shortstop Xander Bogaerts to get Altuve in the seventh. He was called out, and replays were inconclusive on if the tag was applied before he touched the plate with his left hand.

“He did get in there. It’s clear as day,” Hinch said. “I’m tired of these questions because replay’s set up for precisely that type of play, and we feel like we have clear evidence.”

CLUB MILESTONE

Martinez became just the fourth Red Sox player to hit 40 homers in his first season with the team.

There was a large contingent of Astros fans all weekend, filling Fenway Park, restaurants and bars in the area. The Houston Texans faced the New England Patriots about 45 minutes away in Foxborough, Massachusetts, earlier in the day.

TRAINER’S ROOM

Red Sox: Lefty Chris Sale is still set to be activated off the disabled list and start Tuesday. Cora said the plan is for the ace to go about two innings the first time as he gradually works back from mild left shoulder inflammation that landed him on the DL twice in less than three weeks.

UP NEXT

Astros: RHP Justin Verlander (14-9, 2.73 ERA) is scheduled to pitch in Detroit on Monday for the first time since being traded last season by the Tigers. LHP Francisco Liriano (4-9, 4.90) is slated to start for the Tigers.

Red Sox: After an off day, Sale (12-4, 1.97) is set to face Toronto LHP Ryan Borucki (3-4, 4.39) when the teams open a three-game series Tuesday at Fenway.