BOSTON – The Boston Red Sox got closer to the playoffs on Wednesday night even as they missed a chance to clinch their first postseason berth since 2009.

As they went into extra innings against the Baltimore Orioles, the Red Sox were guaranteed at least a tie for the second wild-card berth when the Kansas City Royals beat the Cleveland Indians. But the Orioles kept Boston from clinching outright with a 5-3 victory in the 12th inning.

“We know where the playoffs are. We know how far away we are. We’re not stupid,” catcher David Ross said moments after the clubhouse TV showed the Tampa Bay Rays beating Texas — also in extra innings — to hold Boston’s magic number to clinch an AL East title to three.

“So we feel pretty good about our chances.”

Chris Davis singled in two runs in the 12th inning for Baltimore to help the Orioles avoid elimination in the division race. Baltimore is 10 games back with 11 to play.

That’s a good ballclub. We knew at the start of the season they were going to be in it,” said Red Sox starter Jake Peavy. “Nothing comes easy in major league baseball, much less in the (AL East).”

Matt Wieters had a pair of RBI doubles for Baltimore, which closed to one game back of the Rangers, who lost 4-3 in 12 innings to the wild card-leading Rays, for the AL’s second wild-card spot.

Despite the loss, the Red Sox reduced their magic number to one for clinching their postseason berth when the Cleveland Indians lost to the Kansas City Royals 7-2 on Wednesday.

T.J. McFarland (2-1) worked the 11th for the win and Jim Johnson got the final three outs for his AL-leading 47th save.

The Orioles played their 114th errorless game, surpassing the 2008 Houston Astros for the majors’ most since 1900.

J.J. Hardy and Brian Roberts had consecutive one-out singles and both advanced on a wild pitch by Franklin Morales (2-2). After pinch-hitter Steve Pearce was intentionally walked, Manny Machado fouled out before Davis’ grounded one up the middle into center field.

Boston had runners on first and second with one out in the 11th, but Stephen Drew grounded into a double play.

David Ortiz hit his 28th home run — a two-run shot — and Mike Napoli added a solo homer for the Red Sox.

Baltimore had pulled in front 3-2 in the sixth on Wieters’ RBI double, but Napoli hit his solo homer into the center field bleachers leading off the bottom of the inning against Wei-Yin Chen.

Boston jumped ahead 2-0 in the first on Ortiz’s long homer into the seats about 15 rows back in right field. Victorino singled before Ortiz hit the first pitch, holding his bat and admiring his drive for a while before rounding the bases.

Peavy went seven innings, allowing three runs, six hits, striking out eight and walking one.

NOTE: Boston CF Jacoby Ellsbury, sidelined for his 11th straight game after breaking a small bone in his right foot, said he’s still planning to be back by the end of the regular season.

“I’m making great progress. The doctors are pleased with how things are going,” he said.