KANSAS CITY, Mo. – Dustin Pedroia drove in three runs with a pair of well-timed singles, Josh Beckett survived a shaky start to go seven innings, and the Boston Red Sox got back on the winning track Thursday night with a 4-3 victory against the Kansas City Royals.

Jason Varitek drove in the other run for the Red Sox, who had lost five of their last seven games after getting shut out Wednesday night by the division rival Tampa Bay Rays.

Beckett (10-5) and the Royals proved the perfect matchup to turn things around. He allowed all three runs in the first three innings to improve to 7-1 in his career against them — his only loss came July 28 in Boston. Beckett also reached 10 wins after failing in his four previous attempts.

Daniel Bard worked the eighth for Boston, and Jonathan Papelbon made it through a perfect ninth to extend his career-best streak to 24 consecutive saves. It was his 29th of the season.

Luke Hochevar (8-10) labored through 114 pitches in just six innings for the Royals. The former No. 1 overall draft pick allowed all four runs on eight hits and two walks, despite getting some help from his defense.

The Royals threw out three Red Sox runners: Varitek was nabbed at second trying to stretch a single in the second inning; Crawford was thrown out by center fielder Melky Cabrera trying to score from third on a shallow fly ball in the fourth; and Pedroia was thrown out at second by catcher Salvador Perez, who made an alert play after a late throw to the plate on Pedroia’s RBI single in the fifth.

Alex Gordon hit a tying two-run homer in the third for Kansas City, and also scored on a sacrifice fly by Billy Butler in the first.

Boston didn’t waste any time matching Kansas City in the second. Josh Reddick doubled leading off and came home on Varitek’s two-out single. The Red Sox then pulled ahead in the third when Mike Aviles singled against his former team, Ellsbury walked, both advanced on a groundout and came scored on Pedroia’s single.

Alcides Escobar led off the bottom half of the third with a single, and Gordon followed by slapping a pitch from Beckett over the left-field wall to tie the game. His 16th homer matched a career high.

The Red Sox nearly pulled ahead in the fourth when Crawford reached on a single, stole second, then stole third — replays showed he should have been called out. Aviles lofted a shallow fly to center moments later, and Cabrera caught it on a jog and unloaded toward home in one motion. Perez fielded the throw, turned and braced himself as Crawford barreled into him at the plate, holding on for the out.

It was the 21st time a Royals outfielder has thrown out someone at the plate, leading the major leagues.

Undaunted, the Red Sox pulled ahead in the fifth. Jed Lowrie doubled with one out and came around to score on a two-out single by Pedroia, who added a double in the eighth for a three-hit game.

Beckett and the bullpen made the lead stand the rest of the way.

NOTES: Boston designated left-hander Randy Williams for assignment to clear a roster space for Ryan Lavarnway.