SEATTLE (AP) — Guillermo Heredia provided the early punch with a home run, then turned an extra 90 feet into the winning run for the Seattle Mariners some four hours later.

Heredia went from first to third on a wild pitch and then came home when Jean Segura rolled an RBI single up the middle with two outs in the 13th inning to cap a two-run rally and give the Mariners a 6-5 victory over the Boston Red Sox in a game that ended early Wednesday morning.

"In my opinion, the biggest play in the game was him going from first to third on the wild pitch, keeping his up head up there and taking the extra base, which allowed him to score the winning run," Mariners manager Scott Servais said. "A lot of crazy plays in that game, but it says a lot about the effort of our ballclub."

Mitch Haniger walked with one out in the 13th off Doug Fister (0-5), pitching his third inning, and was forced at second on Ben Gamel's fielder's choice. Heredia, who had a three-run homer in the second, singled Gamel to third. Gamel scored on a wild pitch to tie it, with Heredia advancing all the way to third. Mike Zunino then walked. Shortstop Xander Bogaerts fielded Segura's roller behind second, but his off-balance throw was way late.

"Obviously, I didn't know right away. I was aggressive on the play," Heredia said through a translator. "Once I looked back at the catcher, he was a little careless on it, I took off for third."

The Red Sox, who stranded two runners in the eighth, ninth and 11th innings, had taken a 5-4 lead in the top half when Sandy Leon singled home Hanley Ramirez with two outs off Tony Zych (5-2).

"Our bullpen did a great job of extending it, we had opportunities throughout, we fight back from 3-0, unfortunately the ending is what it is," Red Sox manager John Farrell said. "It's a tough loss, particularly the way we've scuffled offensively for a period of time now."

Zunino opened the seventh inning with his 15th home run to bring Seattle even at 4-4.

The Red Sox capitalized on a sudden loss of command by starter Felix Hernandez for three runs in the sixth to erase a 3-1 deficit.

Highly touted prospect Rafael Devers, making his debut, walked to open the inning and Andrew Benintendi drew a one-out walk. Both runners advanced on a wild pitch and Dustin Pedroia followed with a two-run double. Pedroia advanced on Ramirez's flyout and came home on Bradley Jr.'s single.

Heredia's three-run homer off starter Drew Pomeranz staked the Mariners to a 3-0 lead in the second.

Ramirez cut it to 3-1 in the fourth with 17th home run, a two-out shot to left.

"We knew it was going to be a tight game. It got a little longer than we expected, but we'll take it," Servais said.

The 20-year-old Devers, who began the season at Double-A and then was called up Monday after just nine games at Triple-A Pawtucket, flied out to center in his first at-bat, walked, hit into a double play in the seventh, and walked again in the ninth. He struck out in the 11th to end the inning with the go-ahead run at third and flied out to center to end the 13th. He finished 0 for 4 with two walks.

"In the first inning I was very nervous, but thank God I was able to get my feet under me," Devers said through a translator. "For me, it's just going out there and playing my brand of baseball and having fun out there, that's what I was trying to do and I think I did that. I'm not happy that we lost, but I'm happy for my first big-league game.

RED SOX TRADE:

Boston acquired INF-OF Eduardo Nunez from the San Francisco Giants in exchange for minor league RHPs Shaun Anderson and Gregory Santos, GM Dave Dombrowski announced mid-game. Nunez, 30, hit .308 with 20 doubles, four home runs, and 31 RBI in 76 games for the Giants this season.

RED SOX MOVES

Dombrowski also announced several moves following the game. LHP Luis Ysla, currently at Double-A Portland, was designated for assignment to make room on the 40-man roster. RHP Blaine Boyer is expected to be activated off the 10-day DL (right elbow strain) on Wednesday. ... RHP Ben Taylor is scheduled to be placed on the 10-day DL, retroactive to Sunday.

TRAINER'S ROOM:

Red Sox: RHP Joe Kelly, on the 10-day DL (left hamstring strain) is getting closer to returning. "That was an encouraging bullpen by Joe today, 25 pitches, 80 to 85 percent," manager John Farrell said. "His next bullpen will be on Friday when we get back home, so he's making pretty good progress." Kelly likely will need at one least rehab outing before returning, Farrell said.

Mariners: CF Jarrod Dyson, who sustained a hyperextended toe when crashing into the wall Saturday, missed his third straight game, but was improving.

UP NEXT:

Red Sox: LHP Chris Sale (12-4, 2.58) closes out the three-game series Wednesday afternoon. Sale has gone at least six innings in all but one of his 20 starts. He has not allowed an earned run in three of his last four starts. Sale leads the AL with 200 strikeouts.

Mariners: RHP Andrew Moore (1-2, 5.70) has not won in four starts since a victory in his debut on June 22. Moore, the Mariners' second-round pick in 2015, has allowed nine home runs in 30 innings.