ST. PETERSBURG, Fla. -- James Shields and the Tampa Bay Rays got the sweep they needed to charge back into the playoff race.

Shields came within two outs of his 12th complete game this season, B.J. Upton hit his first grand slam and the Rays routed the fading Boston Red Sox 9-1 on Sunday.

Tampa Bay moved within 3½ games of the AL wild card leaders, sweeping the three-game series to go a season-high 17 games over .500 at 81-64. The Rays have won 21 games in a row when scoring five runs or more.

"We needed to win these," manager Joe Maddon said. "There's no other way to look at it. Under the circumstances, you've got to do what we did or it's pretty much almost impossible to recover. Our guys believe we can do this. It's truly not impossible."

Tampa Bay was 10 games behind the New York Yankees in the wild-card standings on Aug. 7.

Boston has lost five consecutive games, its longest skid since opening the season 0-6, and nine of 11 overall. The teams play each other four more times, in a four-game series beginning Thursday night at Fenway Park.

"We're kind of in a fight right now, we know that," Red Sox manager Terry Francona said about his team's recent play. "It's not real pretty. We'll come out and fight, and hopefully play better. I always feel like we're going to play well and when we don't, we're going to fix it. I still feel that way."

Shields (15-10), who has won four consecutive starts en route to his career-best 15th win, allowed seven hits over 8 1/3 innings. Coming off a 5-1 complete-game victory over Texas last Monday, the right-hander was replaced by Dane De La Rosa after issuing a one-out walk in the ninth on his 121st pitch.

"We're back in the hunt," Shields said. "They know that we're right behind them."

The Rays went up 8-1 on Upton's 20th homer in the fifth off Matt Albers.

Upton finished with four hits and walked once. He and his brother Justin of the Arizona Diamondbacks became the first set of siblings in major league history to both have 20 homers and 20 stolen bases in the same season.

"Couldn't ask for a better day," B.J. Upton said.

Jon Lester (15-7) gave up four runs and eight hits over four innings for the Red Sox. Boston starting pitchers have gone five innings or less in nine of the last 11 games.

Boston is off Monday, while Tampa Bay will begin an 11-game road trip at Baltimore. Rays right-hander Jeff Niemann (9-7) will go against left-hander Zach Britton (9-9).

"I don't think there will be a letdown," Niemann said. "I think everyone knows what our goal is here. Take care of business. ... We have to win."

The Red Sox did get some encouraging news: Francona said Josh Beckett, out with a sprained right ankle, is scheduled to throw off a bullpen mound Monday.

After loading the bases with no outs in the first, Tampa Bay took a 2-0 lead on Ben Zobrist's two-run single off Lester. Sean Rodriguez made it 3-0 later in the inning with a sacrifice fly.

Lester had allowed one earned run or less in his previous five starts, going 4-0 during that stretch. The left-hander threw 43 pitches in the first and 111 overall.

Rodriguez put the Rays up 4-1 with an RBI double in the third.

Marco Scutaro got the Red Sox within 3-1 on a third-inning homer. It stopped a personal stretch of 161 at-bats without a home run, dating to July 15 against Tampa Bay.

Shields worked out of a bases-loaded, one-out jam later in the third when David Ortiz flied out and Josh Reddick was retired on a foul pop.

The Red Sox had two on and one out in the second, but Jason Varitek hit a double-play grounder.

Boston's Jacoby Ellsbury, who turned 28 on Sunday, extended his hitting streak to 16 games with a fifth-inning single.

Game notes

Rays closer Kyle Farnsworth (right elbow) will be re-evaluated Tuesday. ... Boston RHP John Lackey (bruised left calf) is listed to make his next scheduled start on Wednesday. ... Red Sox RHP Clay Buchholz (back) threw at 60 feet on level ground and could soon be ready to throw off a bullpen mound. ... Tampa Bay purchased the contract of utility man Russ Canzler and recalled De La Rosa from Triple-A Durham. RHP Mike Ekstrom was designated for assignment. ... Boston RHP Tim Wakefield (6-6), still looking for his 200th career win, will face Toronto RHP Brandon Morrow (9-10) on Tuesday night.