ST. PETERSBURG, Fla. — Timely hitting and another stellar job by the bullpen. After a rough June, the Tampa Bay Rays seem to be getting their act together again.

“I like the way we’re playing,” Manager Joe Maddon said after Monday night’s 6-5 victory over Boston moved his team past the Red Sox into second place in the AL East.

The Rays improved to 4-1 in July after going 11-14 last month and losing the two-month grip they held on first place in the division.

Jason Bartlett’s sacrifice fly in the seventh inning snapped a tie and Tampa Bay rallied from a four-run deficit against right-hander Daisuke Matsuzaka to beat the Red Sox for the sixth time in 10 meetings this season.

John Jaso drove in two runs with a base-loaded single that made it 5-5 and chased Matsuzaka in the sixth. Bartlett’s sacrifice fly off Ramon Ramirez (0-2) drove in pinch-runner B.J. Upton after Matt Joyce and Sean Rodriguez singled to open the seventh.

A lack of hitting in key situations undermined the Rays most of June, but hasn’t been a problem in coming from behind to win the past three games.

“It getting back to the way it looked at the beginning of the season,” said Maddon, who watched his team rally in the late innings for victories Saturday and Sunday to complete a confidence-boosting series win at Minnesota.

Randy Choate (2-2) struck out the only batter he faced — bailing the Rays out of a bases-loaded jam in the top of the seventh — to get the win.

Joaquin Benoit pitched a perfect eighth before Rafael Soriano got the final three outs for his 22nd save in 23 opportunities.

Eric Patterson, acquired last month to provide depth for an injury-depleted Red Sox roster, hit a pair of solo home runs off Tampa Bay starter Matt Garza. Kevin Youkilis, Adrian Beltre and Bill Hall drove in runs during a four-run third inning for Boston.

But the Rays’ bullpen held the Red Sox in check over the last five innings, giving an offense that’s come alive over the past week an opportunity to get back into the game against Matsuzaka, who allowed five runs and eight hits in five-plus innings.

The Rays got going in the sixth when Rodriguez doubled off Matsuzaka, Carlos Pena walked and Bartlett laid down a bunt that left Boston in a bases-loaded jam when the pitcher wasted an opportunity to get an out at first.

Matsuzaka picked the ball and looked for a possible force play at third, even though Beltre — the third baseman — was standing beside him.

“Right off the bat it was my intention to make a play at third. I thought I did have a play at third. The catcher and third baseman were screaming at me, but their voices were lost in the crowd,” Matsuzaka said through an interpreter.

“The sixth inning was a tough inning. A lot of baserunners and through all that the bunt ends up being a huge play,” Boston Manager Terry Francona said. “They’re trying to give you an out. Not getting that out ends up being huge.”

Jaso followed with his game-tying single.

Carl Crawford had three of Tampa Bay’s eight hits off Matsuzaka.