New York overcame a three-run deficit with a four-run eighth and went on to snap a four-game losing streak. The Yankees moved within 2 1/2 games of the second wild card spot in the American League.
Toronto took a 3-0 lead in the fourth on a two-run homer by Colby Rasmus and a solo shot by Ryan Goins, the first of his major league career.
New York staged a huge comeback, however, sending eight men to the plate in the eighth. Robinson Cano singled in the first run of the frame, a double by Alfonso Soriano accounted for another and Wells finished things off with a two-run, two-base hit.
It was then left up to Rivera, who has blown seven save chances this season -- five of them coming since Aug. 7.
Rivera gave up singles by Adam Lind and Rasmus to start the ninth, but recorded outs on ground balls from Munenori Kawasaki and Goins. A strikeout on three pitches by J.P. Arencibia brought Rivera his 44th save.
The win went to David Huff (3-2), who worked 3 2/3 innings of one-hit, one-run relief. Steve Delabar (5-5) gave up the third and fourth runs in the New York eighth to take the loss.