NEW YORK — If the Red Sox were despondent about losing to the Yankees on a walkoff grand slam by Mark Teixeira on Wednesday night, it went away by the time they reached the clubhouse and the tubs of champagne were waiting.
The Sox lost, 5-3, against their rivals but partied like champions after clinching the American League East courtesy of the Baltimore Orioles beating the second-place Toronto Blue Jays, 3-2.
After two straight years of finishing in last place, the Sox didn’t care how they clinched, only that they did.
“It’s a little weird celebrating after a loss but we can’t let one inning spoil what we did. We earned it,’’ Mookie Betts said.
It was the ninth division title for the Red Sox. John Farrell joined Joe Morgan as the only Sox managers with two AL East crowns.
Farrell addressed the team before the party started.
“I congratulated them on a tremendous year,’’ he said. “One inning should not take away from the fact that we’re AL East champions. It’s been a hard-fought battle. They should be proud of the work that they put in.
“By no means is this the end. This is just the beginning of our postseason.’’
The Sox will play Game 1 of the best-of-five Division Series next Thursday against an opponent to be determined.
With four games left in the season, the Sox are in a position to have home-field advantage for that series. They have the second-best record in the American League, a game ahead of the Cleveland Indians.
The Sox have the tiebreaker, having won the season series against the Indians.
“It’s unbelievable, man. We’ve come a long way from where the season ended last year. Being able to be in the playoffs right now is something very special,’’ David Ortiz said. “It’s going to be my last one. I’m going to try to enjoy it the most I can.’’
Ortiz then turned away.
“Let me pop! Let me pop a little bit!’’ he said.
President of baseball operations Dave Dombrowski, who is 60, joined his players for the celebration, donning Oakley ski goggles to shield his eyes from the champagne. Several pitchers surrounded him at one point, pouring beer on his head.
“I’m happy for the organization,’’ said Dombrowski, who was hired 13 months ago and charged with building an immediate winner. “This is just the first step.’’
The Sox took a 3-0 lead into the ninth inning only to see Craig Kimbrel walk in a run. Joe Kelly came in with the bases loaded and got two outs before Teixeira homered to right-center.
“Looking at it now, yeah, that’s great. You never want to see a team celebrate on your home field,’’ Teixeira said.
The Sox blew the chance to celebrate on the field in New York for the first time since the 2004 American League Championship Series.
“An unbelievable moment,’’ Yankees manager Joe Girardi said.
Before Kimbrel threw a pitch, the scoreboard flashed the Toronto score and most of the Sox knew they were division champions. But Kelly did not until after the game.
“We were down for about a minute and then I figured it out,’’ the righthander said. “There are not too many times you can give up a walkoff grand slam and be happy about it a minute later.’’
In a scoreless game, the Sox caught a break in the eighth inning when Sandy Leon reached on an error by second baseman Starlin Castro.
Dustin Pedroia drove a ball to the gap in right field and the Yankees were fortunate when it hopped over the fence for a double. Pinch runner Marco Hernandez had to stop at third.
Xander Bogaerts lined out to third base. The Yankees then intentionally walked Ortiz, who was 0 for 8 in the series.
Betts hit the ball into the ground just to the left of the plate and it bounced into left field for a two-run double.
He smiled when he got to second.
“That wasn’t exactly what I wanted to do,’’ said Betts, who has 112 RBIs. “But it worked.’’
The Yankees intentionally walked Hanley Ramirez and called in lefthander Tommy Layne to face Jackie Bradley Jr. Ortiz scored on a passed ball, sliding awkwardly into the plate to make it 3-0.
Before the eighth inning, the pitchers ruled on a cold and rainy night in the Bronx.
Red Sox starter Clay Buchholz was terrific. He went six scoreless innings, allowing one hit with two walks and six strikeouts. The Yankees advanced only one runner beyond first base against him.
After a terrible start to his season, Buchholz has a 2.98 ERA in his last nine starts and earned a spot in the postseason rotation.
Brad Ziegler followed Buchholz to the mound and retired the Yankees in order in the seventh inning. He has a 1.29 ERA in 31 appearances since being acquired from Arizona in July.
Yankees starter Bryan Mitchell allowed two hits in seven scoreless innings. He survived five walks.
Koji Uehara, who pitched a scoreless eighth inning, out-partied many of his younger teammates.
“I love the playoffs,’’ the 41-year-old said in English. “Now we go.’’
Peter Abraham can be reached at pabraham@globe.com. Follow him on Twitter @PeteAbe.