BALTIMORE — Just when it seemed safe to write off Ubaldo Jimenez for good, the beleaguered Orioles right-hander puts together back-to-back strong starts to preserve his place in the starting rotation.

Jimenez, who pitched a strong six innings in a tough loss to the Tampa Rays last Wednesday, came back to deliver an impressive seven-inning performance in the Orioles’ walk-off 2-1 victory over the sizzling Kansas City Royals before 20,663 at Oriole Park.

The Royals came into the game with 10 wins in their last 11 games, but Jimenez held them to just a run on five hits. He struck out six and walked just two to reduce his ERA to 6.56.

The O’s won the game with in the ninth inning on a two-out single by No. 9 hitter Craig Gentry, who poked a grounder through the middle of the infield to scored Caleb Joseph and give the Orioles their third straight victory. It was Gentry’s first career walk-off hit.

The only Kansas City run scored off Jimenez came in the first inning on a pair of solid doubles by Lorenzo Cain and Eric Hosmer. The Orioles came back to tie the game against Royals starter Danny Duffy in the fifth inning, when Manny Machado doubled Duffy turned in a performance that was almost identical to Jimenez’s, pitching seven innings and allowing a run on seven hits. He also struck out six.

Neither starter would be around for the decision. Mychal Givens took over for Jimenez in the eighth and right-hander Peter Moylan replaced Duffy. Zach Britton came on in the ninth for the Orioles and got the victory. Joakim Soria took the loss.

Machado continues to dig out of the low .200s after a long struggle to make his batting average more consistent with his solid run-production numbers. He doubled in two of his first three at-bats to raise his average to .246. Machado ranks 18th among third basemen in batting average, but is among the top 10 in doubles (23) and home runs (18).

Sometimes, it’s better to be lucky than good. Schoop was both on Monday. He delivered another multi — hit game, reaching base on a squirrelly ground ball that eluded to fielders and driving home the game-tying run in the fifth inning with a soft flyball single in the fifth. That raised his team-leading average to .307.

Givens pitched a perfect eighth inning, striking out two, to complete a very impressive month. He allowed just one run in July — on a home run by Tampa Bay’s Jesus Sucre — over 12 appearances and 12.1 innings. He struck out 18 and walked just one.