But in the ninth inning, as the Angels batted around and threatened to complete what was developing into a remarkable comeback, the game and the trip came down to one of the more captivating matchups any sport could provide.

Mariano Rivera, the career saves leader, would face Albert Pujols, perhaps the greatest hitter of his generation, to decide it. It was Wilt Chamberlain against Bill Russell, Joe Montana vs. Lawrence Taylor.

The Angels had loaded the bases and the Yankees’ lead, which was six, had dwindled to a run. With one successful swing by Pujols, the 11th batter the Angels sent to the plate in the inning, even a bloop over an infielder’s head would have turned an already awkward trip for the Yankees into a calamity.

“That would have been one of the toughest games of the season if we would have lost,” Vernon Wells said. “But Mo prevailed.”

Rivera, who made his major league debut in this stadium in 1995 and earned his first save against the Angels a year later, struck out Pujols on three pitches — all two-seam fastballs — to earn his 632nd save.

The Yankees held on with their fingernails to win, 6-5, and spare themselves a season-high sixth straight loss; instead, they finished the trip on a high note, with a 4-6 record.

The westward journey began in Seattle, where the Yankees won three of four against the Mariners. Combined with their three-game sweep of the Cleveland Indians at home, it gave the Yankees a 37-26 record over all. But things changed drastically when they arrived in Oakland. The Yankees lost all three games in that series, culminating with Thursday’s agonizing 18-inning loss.

The next day they arrived in Anaheim, lost Kevin Youkilis to a recurring back injury, and then scored two runs in each of the first two games to extend their losing streak to five games, matching their season high. They had lost six straight games only once previously in the last six seasons.

On Saturday, Mark Teixeira flew back to New York because his injured right wrist was bothering him. (Tests revealed inflammation, and he will miss a few days, the Yankees said.)

This was the background as C. C. Sabathia took the mound, looking to will the Yankees to a victory despite the recent stagnation of the offense, which had scored two runs or fewer in six of the Yankees’ previous eight games.

Sabathia was up to the task, pitching marvelously through eight scoreless innings. And the offense broke through, too. Travis Hafner hit a three-run homer against Jered Weaver in the third inning, relaxing his teammates, who scored twice more in the inning. They added one more run in the eighth on a sacrifice fly by Wells, a run that proved decisive.

Lyle Overbay, playing in place of Teixeira, also had a hit, a run batted in and scored a run as the B Squad Yankees, so nicknamed by the hitting coach Kevin Long, came through.

Sabathia came within three outs of his first shutout of the season and second complete game. But he was replaced by Dave Robertson after allowing a leadoff double to Mike Trout in the ninth and then walking Pujols.

As he walked off the mound with two runners on base, Sabathia put his glove to his mouth to cover whatever expletive he was about to utter, then brought his hand down and let loose with a two-fisted expression of disgust.

“I was just frustrated with myself,” he said. But Manager Joe Girardi said it was one of three huge performances by Sabathia this year, and one the Yankees really needed.

Mark Trumbo then hit a line drive off Robertson that deflected into right field, scoring Trout. Robertson got only one out and Rivera was called upon with the tying run on deck.

In one of his least sharp outings of the season, Rivera gave up a two-run single to Alberto Callaspo and a run-scoring single to Peter Bourjos. Then he walked Trout, bringing Pujols to the plate while fans of both teams roared for their champion. Even players in the field appreciated the moment.

“It’s the greatest closer ever against one of the best hitters of our generation,” Wells said. “You definitely take in those moments, and we were glad our guy won.”

Suspecting Pujols was looking for his signature cut-fastball, Rivera instead threw him all two-seam fastballs, or sinkers, which dive in the opposite direction as his cutter. Pujols took the first pitch for a called strike, fouled one off, and then went down on a check swing on a pitch up and in.

After Rivera had saved the inning, the game and the trip, he was typically modest.

“That’s why baseball is so special and unique,” he said. “Just when you think you’ve figured it out, something always happens.”