Red Sox closer Jonathan Papelbon celebrates the last out in Boston’s 8-7 victory over the Orioles at Camden Yards. (Rob Carr/GETTY IMAGES)

It is difficult to remember now, in the midst of all the carnage and shame of the past month, but for a long, long stretch of this season — basically, 41 / 2 months’ worth — the Boston Red Sox were indisputably the best team in the American League, if not all of baseball. Between April 16 and Aug. 27, they went 80-41. And then, for reasons that are still not completely clear, the wheels fell off.

Somewhere in there, buried beneath all the frazzled nerves, bandage-wrapped appendages and wounded egos, that team — or at least several dismembered parts of it — still lives and breathes. And Tuesday night, in the regular season’s penultimate game, at the end of a hellish month that has the Red Sox on the verge of infamy, in a game they would have sold their souls to win, it made a timely reappearance.

One night after losing the last of their once-insurmountable nine-game lead in the American League wild-card race, the Red Sox bashed four home runs — two of them, improbably, by rookie catcher Ryan Lavarnway, in his first big league start behind the plate — on their way to an 8-7 win over the Baltimore Orioles at Oriole Park at Camden Yards. It was just the seventh win this month, against 19 losses, for the reeling Red Sox.

Although the hard-charging Tampa Bay Rays won again in St. Petersburg, Fla., against the New York Yankees, 5-3, to remain tied with Boston in the wild-card race, for the Red Sox the simple act of winning a game was enough to remind them that, as bad as things have been, they still control their own fate.

“We’re playing for something,” Red Sox second baseman Dustin Pedroia said. “A lot of teams don’t get to say that.”

The wild card, thus, will come down to game No. 162, and perhaps No. 163. On Wednesday night, the Red Sox will start ace lefty Jon Lester on three days’ rest in Baltimore, while the Rays will send lefty David Price to the mound against New York. If both teams win, or both lose, they will meet in a one-game playoff Thursday at Tampa Bay. The Red Sox remain in mortal danger of becoming the first team in history to blow a lead of nine or more games in September, but all they have to do to avoid such a fate is win.

“I think it’s really good for baseball,” Red Sox Manager Terry Francona said of the prospect of meaningful baseball in game 162. “It’s not so good for my stomach.”

Of all the unlikely heroes to resuscitate the Red Sox’s season, Lavarnway may have been the longest shot of all. A 24-year-old Yale product — a philosophy major — he had seen just nine innings behind the plate in his big league career, all of them in the past five weeks. That he was playing at all was a function of necessity, as regular catchers Jarrod Saltalamacchia and Jason Varitek were nursing injuries.

But Lavarnway, who hit 32 homers in only 116 games in the minors this season, connected for a three-run homer off Orioles starter Zach Britton in the fourth inning, pushing the Red Sox’ lead to 5-1, then added a solo shot in the eighth. The Red Sox also got two-run homers from Jacoby Ellsbury and Marco Scutaro.

“It feels good. It feels like I can wear the jersey with pride,” Lavarnway said. “I didn’t want to be just a September call-up that was meaningless.”

The Red Sox still have problems — huge, intractable ones — that were scarcely obscured by Tuesday’s win. Chief among these is their starting pitching. Desperate for a win Tuesday, they had little choice but to start Erik Bedard, a 32-year-old lefty acquired at the trade deadline for depth. Given his recent injuries and track record — and the lack of a better option — the Red Sox would have been thrilled to get five serviceable innings from him. But even that was too much to ask — Bedard couldn’t even get through four, requiring 84 pitches to secure just 10 outs.

One of the great mysteries of this season is how the Red Sox, with their vast resources, could struggle just to find enough warm bodies to populate a pitching staff. This month, their rotation has put up a ghastly 7.28 ERA.

There would also be drama for the Red Sox at the end of the game, with setup man Daniel Bard surrendering a pair of runs in the eighth, and closer Jonathan Papelbon allowing one more in the ninth. But with the tying run on second base, Papelbon got Adam Jones to ground to third at the end of a 10-pitch at-bat to end the game.

“Everything we got, we needed,” Francona said. “I haven’t been that nervous in a while.”

The Red Sox’ playoff hopes are far from safe. In fact, they are technically no better than they were the day before. But on this night, a simple win could be enjoyed on its own merits, and for the simple reason that the Red Sox had to have it.