ST. PETERSBURG -- Mark Melancon doesn't give up runs very often, but in the bottom of the ninth inning Tuesday night, two were already in against the Pirates' closer, and Evan Longoria was stepping to the plate as the winning run.

But with one 92-mph cutter, Melancon got the Rays' star lunging, and seconds later, his lazy fly ball was in center fielder Andrew McCutchen's glove, preserving Pittsburgh's 6-5 win at Tropicana Field. It was Melancon's 13th save of the season.

The Pirates got another workman-like effort from their starter, as Jeff Locke went 7 1/3 innings and gave up three runs for his first win of the season, and Pittsburgh beat the Rays for the second straight night. The Pirates also turned two double plays behind Locke, after turning three the night before.

"I'm just throwing strikes," Locke said. "That's all you can attribute it to. One change I've tried to make is to try to get ahead more in the count. You don't have to nibble so much. Put them behind, challenge them a little more, and keep the ball on the ground."

The Pirates moved to 39-38 and are now over .500 for the first time since April 15. They have the best record in the National League since May 6, 27-18. Tampa Bay, meanwhile, is 31-48, the worst record in the Majors.

"We're playing good baseball," Pirates second baseman Neil Walker said. "We don't really worry about what our record is, as opposed to how we're playing."

Melancon, who had allowed just one run in June before Tuesday's game, made things dicey when he allowed a two-out, two-run single to Brandon Guyer in the ninth inning that cut Pittsburgh's lead to one. But for most of the game, the Pirates were in control.

Pittsburgh used small ball to capitalize against the Rays and starter Chris Archer. The Bucs only had one extra-base hit against Archer, but they still hung five runs (four earned) on the Tampa Bay right-hander.

"I think we just tried to stay aggressively selective in the zone. When we got pitches to hit, we were able to hit them and move, do some things on the bases," Pirates manager Clint Hurdle said. "Not trying to do too much. More or less playing aggressive pepper on him."

In the top of the first inning, as they did in Monday's series opener, the Pirates took advantage of their speed on the basepaths, scoring a run without recording a hit. Gregory Polanco led off the game with a walk, stole second and advanced to third when Rays catcher Jose Molina's throw bounced into center field. Polanco then scored on McCutchen's RBI groundout.

"[The Pirates] have the speed to do all those little different things," Rays manager Joe Maddon said. "The top three guys are unique within their lineup -- not everybody has all that. The big thing is to keep them off base."

The Rays didn't. Pittsburgh added three runs in the third with more small ball. After a walk to Pedro Alvarez and an infield hit by Jordy Mercer, Polanco bunted the runners into scoring position. Starling Marte and McCutchen followed with back-to-back singles, plating the pair. And Walker, fresh off the disabled list, drove in Marte with a sacrifice fly for a 4-0 lead.

Marte had to leave the game in the top of the fifth inning with concussion-like symptoms after sliding headfirst into Rays second baseman Sean Rodriguez's knee on an attempted steal of second base. Hurdle said after the game that the team had sent Marte to a hospital to get a CT scan, but had not gotten a report back yet.

Tampa Bay got one run back in the fifth on an RBI double by Molina, after Polanco lost Logan Forsythe's fly ball to right in the Tropicana Field roof and Forsythe ended up with a triple. But the Pirates pushed the margin back to four in the top of the sixth on a two-out RBI single by Josh Harrison, and Russell Martin made it 6-1 in the eighth with a solo home run.

Longoria ended Locke's night when he cracked a two-run homer in the bottom of the eighth to pull the Rays within three on a pitch Locke would like to have back.

"I know there were some pitches tonight we needed to get, we really would have liked them to move a little bit, but they just missed locations, and I think that's why they were able to put the barrel on the ball," Locke said. "Longoria is a good example. He particularly launched that ball."

But when Longoria came back up in the ninth, with a chance to win the game with one swing, Melancon and the Pirates got the pitch they needed most.