Eduardo Nunez began the eighth with a game-tying homer off James Shields (15-12). Following an Eric Chavez strikeout, Brett Gardner singled and Derek Jeter walked, driving Shields from the game. Cano greeted J.P. Howell with a double to deep left-center to make it 4-2.
Alex Rodriguez had an RBI double for the Yankees, who secured at least a wild card berth with the win. A victory in the nightcap, coupled with a Boston loss to Baltimore on Wednesday, would seal the Yankees' 17th AL East title.
"It's important, but we're playing to win a division," Yankees shortstop Derek Jeter said of the team securing a playoff spot. "It's good to know we'll be playing in the playoffs, but we still have some work to do."
Desmond Jennings hit a two-run homer for the Rays, who fell 2 1/2 games behind the Red Sox in the AL wild card race.
Tampa Bay had an opportunity to add to its 2-1 lead in the top of the eighth, placing runners at first and second with one down. Ayala relieved Boone Logan and struck out Ben Zobrist and Reid Brignac to keep the Yankees, who locked up their 16th postseason appearance in the last 17 years, within striking distance.
Shields was dominant through seven frames, but Nunez turned on his second offering -- an 0-1 changeup -- to begin the home eighth, sending it over the wall in left for a 2-2 tie.
"He threw me a changeup," said Nunez. "I waited for something slow all day long."
Chavez went down swinging and Gardner fell behind 0-2, but battled back, fouling off a curveball and working the count full before ripping a base hit to left. His 46th steal of the season preceded Jeter's four-pitch walk.
Cano then sent Howell's 3-1 slider deep to left-center to plate the go-ahead runs.
Rivera, who became the all-time saves leader on Monday, set down Jennings, B.J. Upton and Evan Longoria in the ninth to record the 603rd rescue of his career.
The Yankees grabbed a 1-0 lead in the first when Jeter singled up the middle and Rodriguez doubled to deep center with two down, but Hector Noesi was unable to hold the advantage.
Scheduled starter Phil Hughes was a late scratch due to back spasms, prompting the need for Noesi's first career major league start.
In the top of the third, Elliot Johnson led off with a ground-rule double and Jennings followed two batters later with his 10th home run of the season.
Later in the frame, Noesi was relieved by Raul Valdes after Longoria's two-out single.
"We definitely left some opportunities out there," Rays outfielder Johnny Damon said. " I got some good pitches to hit, but swung and missed."
Noesi lasted 2 2/3 innings, allowing two runs on four hits...Shields was charged with four runs on six hits over 7 1/3 innings. He walked two and struck out seven...While tWednesday's opener was a scheduled game, the nightcap is a makeup of an August 14 rainout...The Yankees are 13-7 in the month of September. They won the opener of this four-game series 5-0 on Tuesday. They are 42-12 in day games this season, which marks the best record in the major leagues.