Judge's 22nd career multi-homer game helped the Yankees come back from a 4-1 deficit after Isaac Paredes hit his fourth home run in two games for the Rays.
Clarke Schmidt (4-2) got the win and Clay Holmes pitched a scoreless ninth for his 12th save in 13 attempts.
The win was the 18th in 21 games for the Yankees, who maintained a 12-game lead in the AL East.
"It was a huge series win," said Trevino, who had two of New York's seven hits. "These guys are a good team. We come in here and we take two out of three from 'em."
Trevino's sixth home run provided the Yankees with their winning margin, but it was a defensive play the catcher made in the fourth that turned the game around. The Rays were up 4-1 and had the bases loaded when third baseman Josh Donaldson snuck behind Taylor Walls, and a perfect pickoff throw from Trevino ended the inning.
"That was a shot in the arm," manager Aaron Boone said. "That was just one of those complete, turn-the-tables, pick-me-up plays."
"I was just trying to get (pitcher Jordan Montgomery) out of the inning," Trevino said.
"It was huge getting us out of the inning that way," said Montgomery, "and it gave us a lot of energy, I guess."
Tampa Bay lost for the eighth time in 11 games, and in dropping two of three to the rival Yankees lost a fourth straight series for the first time since August 2017.
Paredes, who homered three times in the Rays' 5-4 win Tuesday night, hit his ninth homer of the season leading off the second against Montgomery. Vidal Brujan added a two-run homer in the same inning to put Tampa Bay up 3-0.
Rays starter Shane Baz pitched 4 2/3 innings in his sixth major league start, giving up one run and four hits while striking out six. He struck out Judge with the bases loaded on his final pitch.
The Yankees left the bases loaded in the fifth and sixth while scoring only one run. But Judge, who had homered in the fourth off Baz, led off the seventh with another home run off Colin Poche, cutting the Rays' lead to 4-3.
Trevino's home run came off Ralph Garza Jr. (0-2) after Isiah Kiner-Falefa led off the eighth with a walk, one of eight issued by Rays pitchers over the final five innings.
"We had opportunities to add more runs and we didn't, and it's fairly obvious that we were a little gassed in the 'pen from the night before and even from previous usage, and it caught up to us there at the end," Rays manager Kevin Cash said. "The walks - it goes without saying that team is going to capitalize on opportunities."
Montgomery gave up more than three earned runs for the first time in his 14 starts this season. He permitted four runs and nine hits in six innings.
"I didn't really have much going there in the middle of the game," he said.
Yankees: RHP Domingo Germán (right shoulder impingement syndrome) gave up one hit in three shutout innings in his first rehab start for Class A Tampa.
Rays: SS Wander Franco (right quad strain) played six innings in the first of his three scheduled rehab games with Triple-A Durham. ... After his fourth rehab outing for Durham on Tuesday, RHP Luis Patino (left oblique strain) will need at least three more, according to Cash. ... RHP Andrew Kittredge underwent Tommy John surgery.
Paredes became the first player in Rays history to homer in four straight at-bats, and the first major leaguer to do it since Jose Abreu of the Chicago White Sox in August 2020.
Paredes' streak of home runs was interrupted Tuesday when he was hit by a pitch in his fourth plate appearance.
Yankees: RHP Jameson Taillon (8-1, 2.70 ERA) attempts to become the AL's first nine-game winner Thursday night in the opener of a four-game series against the Houston Astros at Yankee Stadium.
Rays: LHP Jeffrey Springs (3-2, 2.00) pitches Friday night in the opener of a three-game series against the Pittsburgh Pirates.
Judge demolishes homers 26 and 27 vs Rays
Aaron Judge's MLB home run lead reaches 27 as he hits two against the Rays.