Baseball can drive you nuts. The trick is not to let it. The Braves were swept in San Diego, which might have been galling if the Braves hadn’t left for home fully believing they had outplayed the Padres. Still, a losing streak was born.

It died Saturday, none too soon, in the ninth inning of a game the Braves tried to mess up. They were charged with two errors and made at least three more misplays. They actually did some good things — B.J. Upton hit two emphatic home runs, and the Braves as a team went five whole innings without striking out — but still they entered the bottom of the ninth trailing the reigning World Series champion 5-4.

But here’s where baseball can go from making you crazy to leaving you laughing. Two outs from losing a fifth consecutive game, the Braves conjured a rally out of next to nothing — two walks, an error and two dinky singles — and went home with a spring in their step.

“We’d lost four in a row,” manager Fredi Gonzalez said, “and it felt like we hadn’t won in two weeks.”

Not to hyperventilate or anything, but the Braves needed this one. They were a good team before they went to San Diego and they remained a good team four losses later, but you never want to start wondering if the baseball gods have taken a dislike to you and yours. If nothing else, this victory was a reminder that the baseball gods are a fickle bunch.

The Giants, who won championships in 2010 and again last fall by prevailing in just this sort of game — Giants fans refer to such hand-wringing tableaus as “torture” — were positioned to win but came unstuck. They were unlucky that second-base umpire Bruce Dreckman (some name, eh?) called Gregor Blanco out on what seemed a successful steal ahead of what would have been Tony Abreu’s RBI double, unlucky that their lead was only a skinny run after 8 1/2 innings.

But the Giants have held such leads before. They flubbed this one. With one out, the pinch-hitting Evan Gattis let three Sergio Romo sliders pass for balls two, three and four. Andrelton Simmons topped a grounder that third baseman Joaquin Arias, playing because the rotund Pablo Sandoval is hurt, let slip under his glove. Shortstop Brandon Crawford couldn’t grab it, either. Arias was charged with an error.

Jason Heyward flicked a Romo fastball into left field. Bases loaded. Justin Upton stepped in, the Justin Upton who has taken a slew of third strikes. With the count 3-2, Romo threw his signature slider. Plate umpire Jerry Meals called it ball four, and it looked to have been. (Replay indicated it broke around the plate.)

Romo, who’s accustomed to getting his way, screamed and pointed at Meals. Upton jogged to first, the score tied. “That’s a pitch that pitchers want called and hitters don’t want called,” Upton said. “That’s his pitch, and he’s mastered it.”

Romo threw two more sliders to Freddie Freeman, who swung at neither. Then Romo threw his fastball, which isn’t very fast, and Freeman served it, not very hard, into right field. As happened Aug. 15, 2011, Freeman faced an accomplished bearded Giants closer — Brian Wilson was the pitcher that night — and beat him.

Said Freeman: “Everyone goes through a skid. If your longest skid is four games, you’ll be all right. We played great in San Diego. We hit the ball all over the place. It just didn’t go our way. … San Diego must have had 40 48-foot hits.”

And now the Braves had won on a day when they coulda/shoulda lost. Even talented teams can profit from the nicely timed jump-up-and-down-and-pound-Freeman-on-the-back frantic finish. Said Justin Upton: “You can get a little momentum from a late-inning win.”

Especially against an opponent that has perfected the art of winning at the right moment. The Braves have a better record than San Fran over the past 3 1/2 regular seasons — the Braves are 314-240 to the Giants’ 307-246 — but have only two blurry-fast playoff exits and one epic September collapse to show for it. This technically wasn’t October baseball, but it was the kind of grinding baseball that can be seen in October.

“We needed a win in general,” said Heyward, who’s skilled at not getting carried away.

Given the way it happened, didn’t it feel as if this game counted double? “From our perspective it doesn’t,” Heyward said. “It’s a good end to a losing streak.”

So let’s leave it at that. If the Braves lose the next four, we’ll forget what happened in this ninth inning. If they wind up in October and they see Romo again, they’ll have cause to remember.