Baltimore took two of three from the Rays, who remained 2½ games behind the first-place New York Yankees in the AL East. It was only the second time in 14 tries that the Orioles won the finale to take a three-game series.
After the Rays used two-run drives by Longoria and Upton to take a 5-4 lead in the sixth inning, Baltimore scored four runs in the bottom half against three Tampa Bay relievers.
Baldelli hit a two-run, pinch-hit homer off Mike Gonzalez in the seventh to make it 8-7.
Alfredo Simon (4-2) worked a perfect eighth and Koji Uehara got three straight outs for his sixth save.
Orioles starter Chris Tillman allowed three runs and six hits in 5 1-3 innings. Recalled from Triple-A Norfolk before the game, Tillman retired the side in order only once and was aided by two double plays.
Riding a six-game win streak, Rays starter Wade Davis gave up four runs and six hits in five innings.
After Tampa Bay wasted a two-out triple by Carl Crawford in the top of the first, Baltimore took a 2-0 lead in the bottom half. Brian Roberts walked and was caught leaning toward second when Davis threw wildly on a pickoff attempt. Roberts ended up on third and scored on a single by Markakis, who advanced on two walks and came home on a sacrifice fly by Patterson.
Zobrist ended a 1-for-15 slide in the third with his eighth home run. In the Baltimore half, Felix Pie walked and Patterson hit a high fly to right that cleared the 25-foot scoreboard for a 4-1 lead.
In the Tampa Bay fourth, Matt Joyce hit a triple and Upton drew a two-out walk before Roberts made a sensational diving catch of a liner to second base that appeared destined to make it to the outfield.
NOTES: Cal Ripken Jr. threw out the ceremonial first pitch to mark the 15th anniversary of the day he tied Lou Gehrig's record for consecutive games played. ... Tillman has yielded 20 homers in his career, 16 of them solo shots. ... Davis' errant pickoff throw ended a run of 10 straight errorless games by the Rays. ... Qualls' run of six straight scoreless outings ended.