BUFFALO, N.Y. -- For the city of Buffalo, it was fun while it lasted.

After not hosting a Major League Baseball game since its days as a Federal League outpost in 1915, Buffalo made the most of its opportunity, hosting the Toronto Blue Jays over two seasons because of coronavirus concerns.

It all ended Wednesday night, as J.D. Martinez and Hunter Renfroe connected back-to-back to help the Boston Red Sox win a home run derby, beating Toronto 7-4 in the final MLB game to be played in the city for the foreseeable future.

In the middle of the third inning, Toronto players and coaches came out of the dugout to salute the fans at Sahlen Field.

In the end, there was no mistaking the finality of the Blue Jays' tenure along Lake Erie as Simon & Garfunkel's "Homeward Bound" played on the stadium's sound system just after Boston finished its on-field celebration.

The Blue Jays will return to Toronto on July 30 after the Canadian government granted them an exemption to the U.S.-Canada travel ban. The Blue Jays haven't played at Rogers Centre since 2019 because of coronavirus protocols. They began the season playing home games in their spring training ballpark in Dunedin, Florida, before shifting up to Buffalo when the more reliable summer weather rolled in.

"It's been a good ride here," Blue Jays manager Charlie Montoyo said. "We're never going to forget that this was the place where we clinched the playoffs last year. The crowds have been great. It was a great idea by our crew [to thank the fans]."

Though there were many nights, especially against the Red Sox and New York Yankees this year, where the Blue Jays might have seemed like the road team in Buffalo, considering the amount of fans both of those clubs brought to the stands.

Wednesday was no different. Red Sox Nation was loud and proud in the stands, as Enrique Hernandez, Rafael Devers and Michael Chavis also connected for Boston. And this was after the Red Sox went deep six times in Monday's 13-4 win over Toronto.

It was the first time the Red Sox hit at least five homers in consecutive games against one opponent since doing it to the Yankees in June 1977.

"We did an amazing job, coming here against a tough team," said Boston manager Alex Cora, whose club scored just six runs in three games in New York against the Yankees before sweeping Toronto. "We ended up 3-2 on this road trip, so it was a positive one for how bad it supposedly looked over the weekend."

Hernandez, who has nine homers in his past 25 games after being held without one in 23 previous games, can't explain the turnaround.

"I don't have a secret," Hernandez said. "I'm just trying to get good at-bats, make good decisions at the pitches that I want to swing at. And it's going well for me right now. I'm just going to try and do my job."

Vladimir Guerrero Jr. hit his 32nd homer for Toronto. George Springer added a two-run shot in the sixth, and Teoscar Hernandez followed with a homer for Toronto, which finished its residency in Buffalo with a 12-11 record this season.

Including games played at the home of their Triple-A team in Florida, the Blue Jays ended with a 29-20 mark as a home team playing in the United States. Their game against Boston on Tuesday night was rained out.

Before the game, Blue Jays president Mark Shapiro presented a check for $25,000 to the Buffalo Bisons Charitable Foundation in thanks for the support provided by Toronto's top minor league affiliate.

Buffalo fans returned the favor, as crowds in excess of 10,000 turned out for six of Toronto's last eight home games, with Wednesday's attendance of 14,607 topping the ledger.

Hernandez hit a two-run shot in the third inning, and Devers and Chavis hit leadoff drives in the fourth and fifth innings. Martinez and Renfroe connected in the eighth as Boston raised its American League-leading road record to 30-19.

Garrett Richards (6-5) allowed four hits in 5 2/3 innings and won a second straight start for the first time since mid-May. He struck out four.

Garrett Whitlock pitched 1 1/3 innings, and Adam Ottavino worked the eighth. Matt Barnes pitched a perfect ninth for his 20th save.

"We pitched well the whole road trip," Cora said. "The bullpen was amazing. [Richards] was pounding the strike zone, changing speeds, using both sides of the plate. We loved what we saw."

Robbie Ray (8-5) allowed the first three Boston homers and struck out four over five innings in the loss, his shortest outing since a 4 2/3-inning stint on June 18 at Baltimore.

Toronto's Danny Jansen left the game after his seventh-inning double, pulling up after rounding first with hamstring tightness. On the hit, Boston's Danny Santana stumbled in left field after trying to reach for the ball and left the game with right groin tightness.

The Associated Press contributed to this report.