Updated

James Shields shook his head in frustration after allowing a walk with two outs in the ninth inning, upset that he might not get to finish a game that he dominated.

Allowed to continue by Manager Joe Maddon, Shields completed his gem with style.

Shields finished off a three-hitter by striking out Gaby Sanchez to set a new career best with 13 strikeouts, and the Tampa Bay Rays beat the Florida Marlins 4-0 Sunday to avoid a three-game sweep.

"I think I put a little stress on Joe right there by walking that guy, but he let me go out there and I commend him for that," Shields said. "I wanted to finish it real bad."

Shields (5-2) walked just one in his second shutout and third complete game of the season. He didn't allow a baserunner past first base until the ninth inning and had nine strikeouts in the last four innings. The right-hander gave up singles in the second, seventh and eighth innings, along with the ninth-inning walk.

"That was really stretching it there, but he really wasn't tested or pushed in that game," Maddon said. "It was an outstanding effort. He told me during spring training he wanted to finish games. He has worked real hard and he deserved that opportunity today."

The victory snapped a three-game losing streak for the Rays, who haven't been swept by their intrastate rivals since 2007.

Florida's Jay Buente (0-1), called up when ace Josh Johnson was put on the disabled list, lasted three innings and gave up four runs — three earned — in his first career start.

Buente allowed five hits and walked three.

"Every time you go out you want to get your team deep in the game and I didn't do that," Buente said. "It's a little disappointing as far as that goes."

Tampa Bay improved its road record to 15-8, best in baseball.

Shields went past the seventh inning for the ninth time in 10 starts this season. It was the eighth time he went at least seven innings while allowing two runs or less. Both marks are tops in the majors.

"It's easy to watch film and stuff, but until you see it firsthand you really don't know what kind of stuff he has," said Rays catcher Kelly Shoppach. "And he has good stuff."

In his only start against Florida in 2010, Shields matched a career high by allowing 10 runs in only 3 1-3 innings of a 14-9 loss at Tampa Bay.

"It depends what day you get somebody," said Florida center fielder Chris Coghlan, who was 0 for 4 with a strikeout. "I don't think we're so bad that he would do a three-hitter shutout against us and I don't think that he's that bad that he would give up 10 runs in an outing. I feel like it's somewhere in between there. We got him that time and he got us this time."

First baseman Casey Kotchman had two hits, including an RBI triple for Tampa Bay. Three-time All-Star third baseman Evan Longoria, mired in a 1-for-18 slump, also had two hits.

"It's great for my confidence going forward," Longoria said. "I just have to continue to have good at-bats and hopefully that that approach will keep carrying over from day to day."

Sanchez had two of Florida's three hits, a sharp grounder up the middle in the second and a blooper in the seventh.

Between Sanchez's two hits, Shields retired 17 consecutive batters.

"I had a lot of (pitches) working very well today," Shields said. "That's a pretty good-hitting team over there and I was able to keep them off balance."

The Rays scored two runs off Buente, who made eight relief appearances for Florida in 2010, in both the second and third innings.

Buente looked as if he might get out of a bases-loaded, one-out jam in the second when he got Shields to hit a grounder to third. But Greg Dobbs booted the potential double-play ball to allow one run to score and Elliot Johnson followed with a fielder's choice to make it 2-0.

Longoria had an RBI double and Kotchman a run-scoring triple in the third.

But the day belonged to Shields.

"That's the story of the game, James Shields," Florida manager Edwin Rodriguez said. "It's the first time I've seen him pitch against us and he was showing command of all his pitches. I think that's the best changeup I've seen this year. He was in command of the game, pretty much from the first inning."

NOTES: Shields' previous career high was 12 strikeouts, which he has done three times — last on April 30 in a no-decision against the Los Angeles Angels. ... After not committing an error, the Rays have gone their first 47 games without a multi-error game. The streak is the second-longest at the start of a season in Major League history and Tampa Bay can match the 2010 Twins' record Tuesday night at Detroit. ... With a walk in the ninth inning, Florida LF Logan Morrison reached base for the 24th time in 24 games this season. It's the longest streak in the majors. ... Tampa Bay RF Matt Joyce, who came into the game second in the majors with a .358 average, went 1 for 4. ... Tampa Bay finished with seven hits, the 10th consecutive game the team was held in single digits.