TORONTO - The New York Yankees rallied with five runs in the ninth inning and then hung on to avoid a four-game sweep as they beat the Toronto Blue Jays 7-5 Monday on an wild evening that saw three hit batsmen, four ejections and the benches empty twice.

For the second night in a row, the game turned in the late innings.

New York's Mark Teixeira tied the game with a one-out solo homer deep to right field off Jason Grilli (7-6), who got the nod with closer Roberto Osuna needing rest. After Didi Gregorius singled, Aaron Hicks went deep to right for a 5-3 lead. Jacoby Ellsbury's RBI single and Gary Sanchez's sacrifice fly off Danny Barnes added to the Jays' pain as the Yankees sent eight men to the plate.

New York had cut the lead to 3-2 in the eighth on Ellsbury's RBI single.

Trailing 7-3, the Jays went to work in their half of the ninth.

Helped by two walks, an error and wild pitch, Toronto loaded the bases with no outs in the bottom of the ninth. And six-foot-eight closer Dellin Betances almost made it four hit batsmen, just missing Darwin Barney with a 97 m.p.h heater.

With Tommy Layne taking over on the mound, Josh Donaldson flied out. Edwin Encarnacion then walked to bring in a run. Pinch-hitter Dioner Navarro's bloop to centre-field made it 7-5. Barney was tagged out at the plate when Russell Martin grounded into a forceout.

Troy Tulowitzki, the last roll of a dice, was caught on a good catch in foul territory to end a game that lasted three hours 20 minutes. Adam Warren (7-4) got the win and Layne the save, his first.

The bad blood started in the first inning when New York starter Luis Severino hit Donaldson on the elbow before a Rogers Centre sellout crowd of 44,532 with the roof closed. Then Toronto starter J.A. Happ took aim at Chase Headley, the first Yankee to go to the plate in the second. It took Happ two pitches to make contact with Headley - he just missed on the first attempt, throwing behind Headley.

Both benches and bullpens emptied but there was no real venom. The umpires cleared the field and the game continued.

Severino, a 22-year-old having a difficult season, then raised the stakes by throwing inside at Justin Smoak to open the bottom of the second. He missed inside but hit Smoak on the leg with the second pitch. That prompted another pitch invasion and there was bad intent this time with a rolling ball of angry players moving across the infield before order was restored.

Catchers Martin and Sanchez had to be separated and Jays reliever Joaquin Benoit limped off the field.

The Jays (86-70) replaced second baseman Devon Travis with Barney in the sixth. There was no immediate word on whether the move was related to the on-field altercations.

Severino was immediately thrown out after hitting Smoak, replaced by reliever Jonathan Holder who had already been warming up. The Yankees starter threw 34 pitches, only 16 of which were strikes.

Yankees manager Joe Girardi, pitching coach Larry Rothschild and bench coach Rob Thomson were also thrown out.

Recent form may have played a role in the bad blood. The Jays are on the rise in the push to the playoffs while the Yankees (80-76) have been sinking fast and had the worst of the first three games of the series.

Lost in the kerfuffle and late-inning collapse was a fine showing by Happ, who was bidding to become just the fourth Blue Jay to win 21 games, joining Roy Halladay (22 in 2008), Roger Clemens (21 in 1997) and Jack Morris (21 in 1992). He was also trying to surpass David Wells (20 in 2000) for most wins by a Toronto left-hander.

Going into Monday's game, the Jays were 23-7 in Happ's starts this season.

Happ exited with one out in the eighth after giving up back-to-back hits. On a night where he deserved better, he gave up six hits with one walk and two strikeouts in a polished 96-pitch performance with 64 strikes.

The game snapped the Jays' three-game win streak and the Yankees' four-game slide.

Toronto holds the top American League wild-card berth with a one-game lead over the Orioles. Detroit is two games back of Baltimore after losing 7-4 to Cleveland. The Indians clinched the AL Central with the win.

Baltimore had the day off before starting a three-game series in Toronto on Tuesday.

Things started poorly for the home side when Brett Gardner's bunt on the second pitch of the game rolled away from Martin and the Yankees left-fielder took third when Martin's throw to first was errant. One out later Sanchez's groundout brought Gardner home.

Toronto answered by loading the bases with one out in its half of the first on a hit batsman, single and walk and went ahead when another Severino walk brought in Donaldson. But a flyout and strikeout rescued the Yankees starter after a rocky 32-pitch inning.

Toronto pulled ahead 3-1 in the third on Tulowitzki's RBI double and Michael Saunders' RBI single.

Joe Biagini and Brett Cecil preceded Grilli for Toronto. The Yankees used eight pitchers in all.

Filling in for the injured Masahiro Tanaka, Severino made sure his first Yankees start since Aug. 14 was an explosive one.

The right-hander, who has had three stints with the Yankees this season, had made 10 starts and 11 relief appearances. He had lost his last six starts, the longest single-season losing streak by a Yankee since Tim Leary lost six in a row in 1990.

Severino was the youngest pitcher to start in the majors last season, debuting against Boston at 21 years 116 days.

Monday's contest marked the close of the Yankees' 12-day, 11-game road trip. They finished their final away swing of the season at 3-8.