In exchange, the Rays received two minor-league prospects -- catcher Michael Perez and right-handed pitcher Brian Shaffer.
The Diamondbacks trail the Los Angeles Dodgers by just a half-game through Wednesday. They also are just a half-game out of the final NL wild-card spot.
Andriese is 3-4 with a 4.07 ERA in 59⅔ innings this season. He has started four games but has mainly seen action out of the bullpen, making 23 appearances in relief. He also has 59 strikeouts and only 18 walks this season, and his average of 8.9 strikeouts per nine innings is a career best.
The 29-year-old Andriese, who is making $558,400 this season and is eligible for arbitration in 2019, is 19-22 with four saves and a 4.30 ERA in four major league seasons, all with the Rays. He posted a career-best eight wins with 109 strikeouts in 2016.
Tampa Bay dealt starting pitcher Nathan Eovaldi earlier Wednesday to Boston for minor left-hander Jalen Beeks.
"I think trades will be made with more of an eye on what we have presently and how to enhance this group further," Tampa Bay general manager Erik Neander said.
Shaffer, 21, is 7-5 with a 2.70 ERA in 19 starts in Class A Kane County this season. He has 109 strikeouts and just 21 walks in 106⅔ innings pitched.
Perez, 25, is hitting .284 with six home runs and 29 RBIs for Triple-A Reno. He also has thrown out 34.8 percent of attempted base stealers.
To make room on the 40-man roster, the Diamondbacks designated RHP Randall Delgado for assignment.
The Associated Press contributed to this report.