If someone were to script devastation, to write out a plot for the near-total destruction of a weary baseball team’s morale, that script would not be nearly as cruel as the one that played out for the Washington Nationals in their 4-3 loss to the Chicago Cubs on Sunday night at Wrigley Field.

As a veteran reliever inched to within one out, then one strike, of securing the most inspiring and encouraging win of this team’s so-far demoralizing season, hundreds of outcomes would have left the Nationals with air in their lungs. The only possible result that could leave them lifeless — the first two-out, walk-off, pinch-hit grand slam to come with a team down three runs in almost four decades — was the one that knocked the wind out of a season that had so little left.

David Bote hit that two-out, two-strike grand slam against Ryan Madson, who was trying to preserve a hard-earned, masterfully crafted three-run lead. Instead of winning a series against the team leading the National League Central and pulling closer to the team leading the National League East than they had been in six weeks, the Nationals suffered a crushing defeat.

Instead of declaring themselves back in the race with a near-perfect performance on national television, instead of taking off for St. Louis on the wings of their best-played game of the season, they slunk through a quiet clubhouse after another night of devastation.

“We left here with one win,” rookie Manager Dave Martinez said, staring out at nothing, his voice as hollow as his eyes. “We should have had three.”

Friday’s game could have gone the Nationals’ way. Sunday’s should have. In fact, at multiple points Sunday, it felt like the day they would vanquish the demons. It felt, quite frankly, like the day the Nationals finally got the breaks they have been waiting for all season.

Although the Nationals got one hit against Cole Hamels in seven innings, they used that one hit to score the only run Max Scherzer needed. As Scherzer dominated, clinging to the kind of one-run lead that one pitch always seems to wipe away for him, he did not break. His 106th pitch of the night struck out Willson Contreras and sent him into convulsions of relief and glee, marching to the dugout like a man electrified by the belief that things might just go right this time.

And when Koda Glover — thrust into high-leverage duty by the trades of Shawn Kelley and Brandon Kintzler and the injuries to Sean Doolittle and Kelvin Herrera — got two outs in the eighth and then made the pitch to get out of it, Javier Baez was initially ruled safe at first. Martinez challenged. The call was overturned, sending Glover to the dugout so frenzied that Martinez said later he nearly broke his manager’s hand with a high-five. The Nationals had reason to believe their closer of the future might be ready to help now.

And in the ninth, when Cubs Manager Joe Maddon told Kintzler to intentionally walk Bryce Harper to load the bases for Ryan Zimmerman, conjuring echoes of agonizing series past, Zimmerman got a base hit up the middle to exorcise those demons and provide insurance for Madson, who has been a reliable setup man and would seem to be a more-than-adequate third-string closer.

“For 8⅔ innings,” Martinez said, “we played really good baseball.”

But ultimately they lost, falling 5½ games behind NL East co-leaders Atlanta and Philadelphia.

Madson got an out before Jason Heyward hit a weak groundball to second baseman Wilmer Difo, who couldn’t handle it, though the play was ruled a hit.

“I’ve seen Difo make that play 99 out of 100 times,” Martinez said. “I’ve seen Madson go out there and throw strikes, not hit two guys.”

After that groundball, Madson hit one man, retired another, then struggled through a jerky at-bat with Contreras in which the Cubs’ catcher stepped in and out of the box, calling time repeatedly. Madson said later that didn’t bother him. He has been working on a new set position, and the Cubs didn’t pick up on it, which left Contreras discomforted in the box. When he finally settled in again, Madson let a curveball slip and hit Contreras in the back to load the bases.

“Didn’t have good Stickum on my fingers today,” Madson said. “Usually have that Stickum, but it wasn’t there.”

Madson, a veteran of World Series teams and respected by many throughout the game, explained that he did not have much Sunday night. He said he really had only his fastball, which is the pitch Bote hit over the center field wall to end it. But most surprisingly, he revealed a previously unmentioned injury, one he said explained the whole thing.

“I’ve been having some back issues that send pain down my right leg, so it kind of cuts the ball off. The speed is still pretty good, but the accuracy is not good,” said Madson, who added he has been dealing with this issue off and on for some time and felt it in the bullpen before the inning. “Everybody’s dealing with things all the time.”

Madson said the trouble comes and goes. He doesn’t know how concerned to be about the problem moving forward. The Nationals, of course, must be very concerned. Their bullpen was not supposed to be the problem again — not this season. How cruel that one of the more devastating losses of the year should come like this.

“It’s a gut punch,” said Scherzer, his voice lower than usual, as if he had yelled it out or simply couldn’t find the energy to lift it. “It’s about how you respond to this.”

For 8⅔ innings, this game seemed like the one that would finally resuscitate the Nationals’ season, once and for all. Now, suddenly, they find themselves struggling to catch their breath — and nearly out of time to do so.