Nationals catcher Wilson Ramos, left, celebrates with Anthony Rendon after hitting a solo home run off Cincinnati Reds starting pitcher Jon Moscot in the fourth inning on Sunday’s 10-9 victory. (John Minchillo/AP)

As he crouched behind the plate, Washington Nationals catcher Wilson Ramos’s heart was racing.

“Really nervous,” Ramos said.

Closer Jonathan Papelbon, on the other hand, is always confident, perhaps even to a fault.

“I never really had a doubt,” Papelbon said afterward. “I truly didn’t.”

Although many might not have shared the same sentiment, Papelbon somehow managed to close out the Nationals’ wild 10-9 win over the Cincinnati Reds on Sunday. He entered in the ninth inning with a two-run lead, after the Nationals’ offense mounted its largest comeback of the season only to have Felipe Rivero nearly squander it. Papelbon then allowed two hits, a walk and a run, and Manager Dusty Baker chose to intentionally walk hot-hitting Jay Bruce.

That meant Papelbon needed to get three outs with the bases loaded in a one-run game. And with a popout, a strikeout and a flyout, Papelbon ran across a tightrope and survived.

“My motto has always been ‘bend but don’t break,’ ” said Papelbon, who is 15 for 17 in save chances this season. Pitching coach Mike Maddux “came out there and told me, ‘We bend, but we don’t break.’ ”

Papelbon’s magic trick preserved the Nationals’ win — their first in Cincinnati since July 27, 2014 — and avoided a sweep. Washington had rallied from a 5-0 deficit in the second inning of Tanner Roark’s rough start. The Nationals started four position players with batting averages under .200 and didn’t notch their first hit until the fourth inning, but the Reds have the worst pitching staff in the major leagues. Their starter, Jon Moscot, entered Sunday with a 7.13 ERA.

“We just stacked good at-bats on top of each other,” second baseman Daniel Murphy said.

The heart of the Nationals’ order, in particular, keyed the comeback. Bryce Harper, Murphy and Ramos each went 3 for 5 and combined to score seven runs. Murphy and Ramos hit back-to-back home runs.

“I’m happy about our comeback,” Ramos said. “That fires a team up. We made the needed adjustments the second time through the order against the pitcher.”

Roark put the Nationals in an early hole in the second when he gave up six hits and walked a batter and Ben Revere bobbled a ball in left field. But the Nationals stormed back in the fourth. Murphy, who hadn’t gone back-to-back games without a hit until this weekend, crushed a two-run home run to right field. Ramos followed with an only-in-Great American Ball Park home run, a lazy flyball to right that carried and landed just over the fence.

After Clint Robinson doubled with two outs, the Reds intentionally walked Danny Espinosa to face Roark. Trailing by two runs, Baker decided he wanted offense. He pulled Roark after the right-hander had thrown only 62 pitches and called for a pinch hitter. Stephen Drew’s RBI single up the middle brought the Nationals to within a run. Revere’s subsequent single tied the score at 5.

“We ended up tying it up right there in that inning, so that’s great ball,” said Roark, who wanted to stay in the game but understood Baker’s reasoning.

Baker was able to use a quick hook with Roark because this three-game series is sandwiched by days off, giving the bullpen time to recover. Left-handed reliever Sammy Solis, who has impressed in Matt Belisle’s absence, provided the needed innings. He tossed three electric frames, striking out six and allowing no runs on 50 pitches.

“Being a [former] starter, just kind of felt fine,” Solis said. “I found my groove, and from there they just kept giving me the ball.”

The Nationals took the lead in the fifth. Harper, Murphy and Ramos all singled to load the bases. Anthony Rendon hit a chopper to third baseman Eugenio Suarez, who made a good throw home to start a potential double play. But catcher Tucker Barnhart whiffed at the catch, which allowed Harper and Murphy to score. Robinson’s sacrifice fly gave the Nationals an 8-5 lead. Murphy and Ramos added RBI singles in the sixth to make it 10-5.

After Solis, Baker turned to Rivero, who has struggled this season against left-handed batters but possessed a comfortable lead. Rivero walked left-handed-hitting Joey Votto, gave up a single to right-handed Brandon Phillips and then faced Bruce, another left-handed hitter. Bruce clobbered an inside fastball to right-center for a three-run home run that trimmed the Nationals’ lead to two runs.

“We got to figure out a way to get Rivero straight against left-handers because we certainly need him because he has dynamite stuff,” Baker said.

Shawn Kelley bounced back from a tough outing Saturday, finishing Rivero’s inning and adding another without allowing a run. This set the stage for Papelbon’s ninth-inning adventure. He entered and surrendered a single to Tyler Holt, walked Votto and allowed an RBI double to Phillips. Both hits came on sliders, but the pitch improved as the inning went on.

Baker didn’t want to face Bruce or hot-hitting Adam Duvall, but he chose to put Bruce on first base. In the dugout, Baker was praying for any type of out. Papelbon then got Duvall to pop out on an inside fastball. He struck out Zack Cozart on a better slider than before. Then Ivan De Jesus Jr. got under a slider and flied out to Michael A. Taylor at the warning track. Papelbon, who said he knew immediately it was an out, pumped his fist forcefully.

“Boy, that was a whew,” Baker said. “It was hard to breathe after that game.”