SAN FRANCISCO -- The final weekend of the San Francisco Giants' season was designed to be a triumphant send-off for Matt Cain into retirement and Johnny Cueto into a potential career-changing decision.
Instead, it turned into a celebration of Pablo Sandoval's return.
Sandoval belted a season-ending home run with one out in the bottom of the ninth inning Sunday afternoon, sending the Giants into the offseason with a 5-4 victory over the San Diego Padres.
The win allowed the Giants to salvage the final series from the Padres after San Diego had taken the first five from its National League West rival this season.
"You have a game like we did (Saturday). That was a pretty good punch in the stomach," Giants manager Bruce Bochy said of Cain's going-away party, which the Padres ruined with a ninth-inning rally.
"To win a game like this says a lot about this team."
In avoiding what would have been just the second 99-loss season in franchise history, the Giants (64-98) prevailed on a day Cueto might have started his final game for the team.
The veteran right-hander can opt out of the final four years of his six-year, $130 million contract in the offseason.
Cueto staggered through the game's first five innings, leaving in a 4-4 tie. He did not get a decision, and finished the season with an 8-8 mark.
"The outing today was the story of Johnny's season," Bochy said, "He competed well without his best stuff. He kept us in the game.
"He hasn't been himself (this season), but he finds a way. The fact that he didn't cave in ... 11, 12 hits ... says a lot about Johnny pitching in traffic."
Padres starter Luis Perdomo enjoyed a historic day himself, even though he, like Cueto, did not get a decision.
Perdomo helped San Diego take an early 4-1 lead, tripling off Cueto and scoring the team's fourth run.
It was his fourth triple of the season. No pitcher had recorded four or more triples in a season since Robin Roberts accomplished the feat in 1955.
Hunter Renfroe had earlier homered for the Padres (71-91), who despite six losses in their last seven games finished three games better than last season, when they were the last-place team in the NL West.
"You're always ready for a break," Renfroe said about season's end. "It gives you a chance to see family and friends. I'm looking forward to a little relaxation period, then pick it up in spring training. We're hoping to make the playoffs next season. That's our biggest goal."
The Giants finished last in the West this season after losing 23 more games than a year ago.
Sandoval's home run came on a full count against the third Padres pitcher, right-hander Phil Maton (3-2). It was the third career walk-off homer for the former Giants star who returned in August off the scrap heap.
"I feel excited for that moment," Sandoval said. "When I was running the bases, I wanted to cry. I want to be here for the rest of my career."
Right-hander Hunter Strickland (4-3), who retired four of the five batters he faced after coming on with two outs in the eighth inning, got the win.
Travis Jankowski had three hits, and Renfroe and Erick Aybar two apiece for the Padres, who won the season series over the Giants 12-7.
The Padres out-hit the Giants 12-8.
Renfroe's homer was the club's 100th on the road this season, the second-most in franchise history.
"We have a lot of guys who can hit the ball out of the ballpark," the rookie insisted. "We don't have a (home) ballpark that's conducive to hitting homers. But we like our ballpark. We hit well there. We're going to break that record next year."
Cueto struggled to get through five innings, allowing 12 hits but just four runs. He struck out two and did not walk a batter.
The 12 hits allowed were a career high for Cueto in a start of five innings or fewer.
The Padres strung together four of those hits consecutively to start the game. After back-to-back doubles by Jankowski and Aybar produced one run, Yangervis Solarte followed a single by Carlos Asuaje with one of his one, making it 2-0 before the game's first out.
Renfroe's homer, his 26th of the season, came in the third inning after the Giants had gotten back within 2-1 on Sandoval's infield out in the second.
The Giants got even in the bottom of the fourth. Crawford doubled in a run and Jarrett Parker had an RBI single before Nick Hundley's fielder's choice brought home Crawford to tie the game at 4-4.
Perdomo retained the tie through seven innings. He allowed seven hits and one walk, striking out four.
NOTES: The Giants held an on-field ceremony for retiring RHP Matt Cain after the game. ... The walk-off win was the Giants' ninth of the season, their most since recording 12 in 2013. ... Giants C/1B Buster Posey had a double and a walk in two plate appearances, raising his season-ending on-base percentage to an even .400. ... Padres 1B Wil Myers did not play in the game. He finished the season with six home runs at AT&T Park, tying Colorado Rockies 3B Nolan Arenado for the most by a Giants opponent in a single season in the 18-year-history of the ballpark. ... Among the Giants to get an early shower was RF Hunter Pence, whose final play was a diving catch in right-center field on a bid for extra bases by Padres RF Hunter Renfroe. Pence got up gingerly and staggered to the dugout, then left for a pinch hitter. ... The Giants finished in a tie with the Detroit Tigers for the worst record in baseball.