BOSTON -- Royals manager Buddy Bell was close to lifting Joey Gathright from the game after he twisted his right ankle while chasing David Ortiz's triple in the third inning.
Good thing Bell didn't.
"Going into the ninth inning, we just felt like we needed to go out there and do something," Gathright said. "And we did. We stepped up."
Mike Sweeney homered and had three RBIs for the Royals. After allowing six runs in the eighth inning and falling behind 9-8, they scored twice against Mike Timlin (6-5) in the ninth to extend their winning streak over Boston to four games.
Emil Brown led off the ninth with a single, Ryan Shealy was hit by a pitch and pinch-hitter Andres Blanco advanced the runners with a sacrifice. One out later, Gathright lined a double down the first-base line.
"It was a huge win for us," Sweeney said. "When we get down in the ninth, usually we crumble. I got the boys fired up and told them, 'Hey we've got to keep charging, this is our game to win.' And we got some big hits."
Kansas City swept a three-game series against the Red Sox at home from Aug. 8-10, starting a 10-21 stretch for Boston during which it fell far behind in the AL East race.
Ortiz had three hits and four RBI for the Red Sox, who dropped 6½ games behind the Twins in the wild-card standings. Jason Varitek homered for Boston.
"It's disappointing, but stuff happens," Red Sox first baseman Kevin Youkilis said. "Things haven't been going well right now. We've got to step it up in these situations."
Ortiz hit his 11th career triple in the third when his shot to center field bounced off the wall toward left. Gathright turned awkwardly and grimaced in pain while left fielder David DeJesus retrieved the ball.
"It wasn't that bad. I just rolled it," Gathright said. "When you roll your ankle you need a few seconds to let it calm down before you come back out."
Ambiorix Burgos (4-5) retired Manny Ramirez to end the eighth. The Royals, who own the worst record in the majors, have won three of five and remained one loss from becoming the first big league team to lose 90 games.
The Red Sox had failed to score more than three runs in 13 of 14 games and struggled against Royals starter Odalis Perez until breaking out in the eighth against four relief pitchers.
Mike Lowell led off the inning with a single and Varitek followed with his 12th homer, a shot into Boston's right-field bullpen. Wily Mo Pena then doubled and scored on Coco Crisp's single, chasing reliever Scott Dohmann.
Following Carlos Pena's strikeout, Youkilis walked and Mark Loretta doubled off Joel Peralta. Ortiz, who had gone hitless in two games since returning to the lineup Tuesday after missing eight games with heart palpitations, then found a hole at shortstop to drive in the go-ahead runs off Jimmy Gobble.
But Burgos got Ramirez to end the inning and Joe Nelson pitched the ninth for his sixth save in six chances.
"The place was jumping because of the eighth inning," Boston manager Terry Francona said. "It would have been an outstanding win. It ends up being a real difficult loss."
The Royals got the best start by Perez since he was traded from the Los Angeles Dodgers on July 25 for right-hander Elmer Dessens. Perez scattered five hits, allowed three runs -- two earned -- and struck out a season-high 10.
He left with a 5-3 lead and the Royals added three more runs in the eighth to make it 8-3 before Boston's eighth-inning comeback.
"Every time I go out there and pitch a good game like that, my confidence builds up and helps me for the next game," Perez said.
Making his second start since being traded from Arizona on Aug. 31 for a player to be named, Boston starter Kevin Jarvis allowed five runs -- four earned -- and 10 hits in five innings. He breezed through the first three innings before the Royals combined for five runs in the fourth and fifth to chase him from the game.
Ortiz was named one of six American League finalists for the Hank Aaron Award, given to the best offensive performer in each league. ... Boston had pitched 24 scoreless innings at home against Kansas City before the Royals scored twice in the fourth. ... Kansas City improved to 11-24 against the AL East. ... Perez's career high for strikeouts is 11. ... Varitek had been 0-for-6 since coming off the disabled list Sept. 3 before going 2-for-4. ... Crisp matched his career high with his 20th stolen base.