When the ball left Ceddanne Rafaela’s bat, everyone in the Red Sox dugout crowded the rail shouting “Pesky! Pesky!” as if to will the ball into fair territory as it flew down the right field line.

“Usually it’s way foul and we’re just searching for something,” outfielder Jarren Duran said later. “But that time it worked.”

Rafaela’s opposite field liner just barely stayed fair, curling around Pesky’s Pole for one of the shortest and most unlikely walk-off home runs in recent memory.

It was a fitting end to one of the most unlikely Red Sox wins of the season.

The Red Sox came from behind on four occasions to pull out a gritty 11-9 win over the Los Angeles Angels. The club overcame a disastrous outing by Lucas Giolito, who became the latest Red Sox starter to crash out early with seven runs allowed over 1.2 innings, and finally put the Angels away for good on Rafaela’s two-run homer in the bottom of the ninth.

After losing so many close games, it was a refreshing breakthrough for a club that needs things to start going its way.

“To say we needed this one is an understatement,” Red Sox manager Alex Cora said.

Rafaela’s home run traveled only 308 feet, which according to the club is the shortest home run hit by a Red Sox player in the Statcast era. It also gave Rafaela three straight games with a home run, punctuating what has been among the most impactful stretches at the plate of his career so far.

“I’ve been feeling good, not only the past few games but since the year started,” said Rafaela, who is now batting .252 with six home runs on the year. “I’ve been feeling good, my confidence is up and I’m going to try to keep helping the team win every day.”

Boston’s bats picked up Giolito, who didn’t have it from the start.

The Red Sox right-hander allowed back-to-back doubles to start the game, the latter coming courtesy of Nolan Schanuel to put the Angels up 1-0 right away. Giolito then surrendered a single to Mike Trout before Taylor Ward took him deep for a three-run home run, his 17th of the season.

Then, after the Red Sox rallied for five runs in the bottom of the first to take the lead, Giolito gave up three more extra-base hits, including a game-tying triple by Zach Neto and a go-ahead double by Schanuel. Ward tacked on a sacrifice fly, and once Giolito walked Jorge Soler, Cora decided he’d seen enough and pulled the beleaguered veteran.

Giolito’s final line: seven runs on eight hits and a walk over just 1.2 innings. He drew only one whiff on 43 pitches, and six of the eight hits against him went for extra bases.

“It was terrible,” Giolito said of his performance. “I’m super proud of the team, everyone came to play today big time, bullpen was incredible, a lot’s being asked of them and I put them in a terrible position.”

“There’s no excuse,” Giolito added. “I’ve got to figure it the (expletive) out.”

Giolito’s debacle was only the latest example of a troubling team-wide trend.

Earlier this week Cora pointed to the pitching staff’s tendency to allow a lot of first inning runs as a major concern, and the numbers back it up. Boston has now allowed 47 runs in the first inning, which is more than any other inning and ranks second-worst in MLB behind only the moribund Colorado Rockies, who have given up 66.

That many first-inning runs means the Red Sox have spent a lot of time trying to climb out of early holes. Normally that’s a dangerous way to live, though Wednesday the club was able to mount a response right away.

After falling behind 4-0, the Red Sox had their first six batters reach safely against Angels starter Jose Soriano in the bottom of the first. Wilyer Abreu broke the ice with a run-scoring single, and after Carlos Narvaez singled to load the bases, Marcelo Mayer drew a walk to record his first career RBI.

Abraham Toro kept the line moving with an RBI single, and David Hamilton delivered the big hit with his go-ahead two-run double to make it 5-4.

Even after falling behind 7-5 in the second, the bats kept putting pressure on the Angels.

Boston loaded the bases with no outs again in the bottom of the fourth after Duran worked an 11-pitch walk against Soriano, blowing up the right-hander’s pitch count and bringing Rafael Devers to the plate. The slugger struck out but the Red Sox capitalized on the chance anyway when Abreu hit a sacrifice fly to deep center field and Carlos Narvaez tied the game with an RBI single.

That also chased Soriano, who allowed seven runs over 3.2 innings. Hunter Strickland came on and got out of the jam without further incident by drawing a flyout from Mayer.

From that point on the sides continuously traded blows.

Los Angeles immediately retook the lead in the fifth after former Red Sox prospect Matthew Lugo reached on a leadoff single, advanced on an error by pitcher Brennan Bernardino and scored on an RBI single off the Green Monster by Logan O’Hoppe, making it 8-7 Angels.

Then, after the Red Sox tied it up once more in the seventh on an Abraham Toro sacrifice fly, the Angels responded again with an RBI single by Kevin Newman in the top of the eighth.

Finally, trailing 9-8 in the bottom of the eighth, the Red Sox tied it again when Trevor Story reached on a fielding error at third and came around to score on a single up the middle by Devers.

At that point the Red Sox badly needed a shutdown inning, and after failing the first three chances, it turned out the fourth time was the charm.

Cooper Criswell, who has hasn’t pitched in the majors since April 6 despite bouncing up and down from Triple-A four separate times since then, kept the Angels off the board and set the table for the Red Sox to finally finish the job in the bottom of the ninth. Toro did his part with a single, and then Rafaela went yard to send the fans home happy.

It won’t get any easier for the Red Sox (30-34). Up next this weekend is the first-place New York Yankees, who mark the beginning of a 15-game stretch for Boston against teams that currently have winning records.

“We go to New York, take that series and see where we’re at and then we come home and hopefully play better here,” Cora said. “But it starts in New York, so we’ve just got to go over there and win the series.”