TORONTO — Clayton Richard had a lot more fun in his third start as a Blue Jay than he did in his other two.

The final scoreline had everything to do with it.

Richard didn't factor into the decision Tuesday, but Toronto's offence ignited behind a pair of homers in the fifth inning to beat the New York Yankees 4-3.

"We won," the veteran left-hander said with a smile when asked what he liked about his outing. "It's a lot better when we win. It's a lot more fun. We can listen to music (in the clubhouse afterwards) and have a good time together."

Richard allowed two runs and four hits with three walks and four strikeouts over 4 2/3 innings, throwing a season-high 87 pitches, 52 for strikes.

The 35-year-old, acquired by Toronto in the off-season, missed the first eight weeks of the season with a knee injury.

"I had a week off so plenty of time to recover from the last (start) and get prepared for this one," Richard said. "Physically I feel great and I'm ready to keep on going."

Freddy Galvis hit a two-run homer and Randal Grichuk also went deep as part of a four-run fifth inning for the Blue Jays (22-38), who snapped a six-game slide. Vladimir Guerrero Jr. hit a run-scoring single and Eric Sogard was 3 for 4 with a double.

Reliever Daniel Hudson (4-2) earned the win while Ken Giles earned a four-out save.

Clint Frazier and Aaron Hicks homered for the Yankees (38-21) in the first meeting between the division rivals this season. Masahiro Tanaka (3-5) allowed four runs and six hits with two walks and two strikeouts.

Tanaka had allowed just one hit and a walk before Toronto's explosive fifth inning.

"I think we stuck to our plan," Galvis said. "We (saw) good pitches and let him make some mistakes and made him pay. I think when a pitcher like that, a good pitcher, makes mistakes you have to make him pay because if not he's going to beat you with the next pitch.

"So I think we took advantage of those pitches and we scored a couple runs right there."

The Blue Jays, whose offence has sputtered all season, came into the game ranked last in the American League in batting average (.218), on-base percentage (.285), on-base plus slugging (.660), and second-last in slugging (.375) and runs per game (3.75).

They've had an especially rough go lately, with back-to-back sweeps on their recent six-game road trip that saw them outscored 37-16.

Asked how much Toronto needed Tuesday's win, Galvis replied: "A lot."

"I think that was a really nice win for everybody. I think we needed that one right there," Galvis added.

Frazier put the Yankees on the board in the fourth inning with a no-doubt blast off Richard over the left-field fence. The homer, which followed a Hicks double, was Frazier's 11th of the season and gave New York a 2-0 lead.

Hicks' second hit of the game was a solo shot in the eighth off reliever Joe Biagini that brought the Yankees within a run.

Toronto escaped a jam in the sixth when reliever Thomas Pannone walked two batters to start the inning. Hudson replaced him on the mound and got the three outs.

"Hudson came in and cleaned that inning and that was huge," Blue Jays manager Charlie Montoyo said. "Our bullpen was very good and Clayton Richard gave us a chance in the four innings he pitched."

NOTES: Canadian golfer Nick Taylor threw out the ceremonial first pitch. ... The Blue Jays reinstated left-hander Tim Mayza from the injured list before the game. ... Canadian left-hander James Paxton, who tossed a no-hitter with Seattle in his last start at Rogers Centre on May 8, 2018, starts the second game of the three-game series for the Yankees Wednesday.