BALTIMORE — Tempers flared when Boston Red Sox reliever Matt Barnes threw high and inside to the Baltimore Orioles’ Manny Machado on Sunday.

The pitch was perceived as retaliation for Machado’s hard slide Friday night that injured Boston second baseman Dustin Pedroia and knocked him out of the next two games. Even though the ball hit Machado’s bat, Barnes was ejected by home plate umpire Andy Fletcher.

The incident marred an otherwise uplifting win for Boston.

Mookie Betts and Hanley Ramirez set the tone with back-to-back homers in the first inning and the Red Sox avoided a three-game sweep with a 6-2 victory.

“He was trying to take a four-seamer in and above his hands and the pitch got away from him,” Boston manager John Farrell said about Barnes’ pitch. “I think any time you see a pinch up around the head, it’s a dangerous pitch. No one likes to see it. If you look at the attack plan of our right-handed pitchers, there’s an area in and above the hands you’re trying to go to. Unfortunately, it does get away from him in that spot.”

Pedroia fell short of saying that Barnes’ pitch was intentional. However, he was adamant that he did not want Machado to get hit because of the hard slide.

“I had nothing to do with that,” Pedroia said. “That’s not how you do that man. I’m sorry to him and his team. If you’re going to protect guys you do it right away. He knows that and both teams know that.”

Barnes also said that he did not mean to throw at Machado, saying: “I would tell Manny I’m sorry. I wasn’t trying to go for your head at all. I was trying to make a pitch and it got away from me.”

Mitch Moreland also went deep for the Red Sox, who entered the game with just two home runs in their past six games. Andrew Benintendi had a career-high five hits for Boston (11-8).

Despite the setback, the Orioles (12-5) still have not lost a series this year.

“It’s two good teams competing for something that a lot of teams really want,” Baltimore manager Buck Showalter said. “And they have the passion and when things like that get going into it, that’s unfortunate. I was really impressed with the courage that our guys showed today because they all, believe me, trust me, wanted to do something about it. Takes a lot more sometimes to do that.”

Red Sox left hander Eduardo Rodriguez (1-1) retired the first nine batters he faced before allowing a walk to Craig Gentry to lead off the fourth. Rodriguez gave up his only hit in the fifth when Chris Davis managed a single.

Rodriguez finished with seven strikeouts and allowed five walks in six innings and a season-high 108 pitches. He has not allowed more than two runs in any of his five career starts at Camden Yards.

The troubles for Orioles starter Kevin Gausman (1-2) started almost immediately when he allowed a three-run homer to Betts, followed by a solo shot by Ramirez. It was Gausman’s second consecutive rough outing. He gave up eight runs in 2 2/3 innings Tuesday against the Cincinnati Reds.

Those struggles carried over against the Red Sox as Gausman allowed five runs and eight hits over 5 1/3 innings. Stefan Crichton entered for the Orioles in the sixth and allowed an RBI single to Benintendi that boosted Boston’s lead to 6-0.

After Barnes was ejected in the eighth, Machado responded with an RBI double off Joe Kelly that gave the Orioles their first run. Hyun Su Kim later had an infield single off Fernando Abad in the ninth that cut the margin to 6-2.

“We lost the game today,” Machado said. “That’s all that matters. Who cares about the situation that happened? It’s not on us. We did the right thing out there, and we’ve got to continue to do what we’ve got to do out there, continue to do the things that we always have been doing and move on to the next series.”

Craig Kimbrel to pick up his seventh save by getting Machado to pop out to end the game. Afterward, though, much of the talk centered around the heated series and whether there might be further retaliation when the team meet May 1-4 against Boston.

“You’ve got to do your best to put that aside,” Orioles first baseman Chris Davis said. “I mean, there’s more at stake obviously. We weren’t happy with the way things went today, not only with what happened late in the game but also losing the game. I think it’s more important to win the game. You can’t get caught up in that stuff.”

NOTES: Boston 3B Pablo Sandoval left the game in the seventh inning with a right knee sprain. He is listed as day to day. … Pedroia (knee, ankle) was out of the lineup for the second consecutive game. He is expected to undergo an MRI on Monday in Boston. … Baltimore RHP Zach Britton (left forearm strain) is scheduled to play catch from 90 feet Monday. He had an MRI on Friday that did not reveal any structural damage.