Medica singled after a 14-pitch at bat, with Guzman watching intently from the on-deck circle. Guzman then homered in a four-run fourth inning.
"I saw a lot of pitches and I hit it good," Guzman said. "He made the pitcher throw a lot of pitches and then he gets the hit."
Hunter Pence, who reached a tentative agreement on a $90 million, five-year contract before the game, hit a two-run homer for the Giants. San Francisco had won three in a row.
Eric Stults (11-13) pitched seven effective innings to win his third straight start after going 0-6 in his previous 10 starts. He also helped himself with an RBI single during a four-run fourth.
"I made some adjustments and was able to follow through on them," Stults said. "Every player wants to end on a good note and sometimes you have to battle the storm a little and keep pushing through."
Stults allowed three runs on seven hits. He did not walk a batter and struck out three.
Yusmeiro Petit (4-1) lasted a season-low 3 2/3 innings, giving up five runs and seven hits.
"I felt good all the way around," Petit said. "This is a learning experience for me because when I get into big trouble I have to find a way to get out of it."
Denorfia led off the game with his career-best 10th home run.
Angel Pagan doubled to open the bottom of the first and scored when Sandoval doubled with two outs.
Pence hit his 27th homer in the third to put the Giants ahead 3-1. His homer total is the highest for the Giants since Barry Bonds hit 28 in 2007.
Petit retired the first two batters in the fourth and then gave up six consecutive hits, including the long at bat by Medica, before being replaced by George Kontos.
"He was throwing everything," Medica said. "He kept making good pitches and I was fortunate to keep getting a piece of them until I finally got one fair."
Chase Headley doubled ahead of Medica. Guzman hit his homer and consecutive singles from Alexi Amarista, Chris Robinson, making his first major league start, and Stults produced another run and ended Petit's day.
Stults, who retired his last nine hitters, threw a career-high 203 2/3 innings this season.
"It's definitely a milestone," Stults said. "The goal was to make it through the season healthy and try to be reliable."
The Padres added four runs in the ninth on Gyorko's 22nd home run, a two-run shot, and RBI singles from Denorfia and Reymond Fuentes.
Gyorko has the most home runs by a rookie second baseman since Dan Uggla hit 27 for the Marlins in 2006.
Giants INF Brandon Belt will be working on his versatility during the offseason according to manager Bruce Bochy. ... RHP Guillermo Moscoso (2-2, 4.21) will start Sunday's season finale for the Giants. It's his first start since Aug. 4. ... RHP Tyson Ross (3-8, 3.10) makes the start for the Padres on Sunday. He has an ERA of 2.80 over his last 12 starts. ... Denorfia hit his second leadoff homer of the season and seventh of his career. ... Medica had his first career three-hit game.
Revisiting the 2016 MLB draft
Kiley McDaniel reflects on why 2016 was a tricky draft year to figure out as players like Bo Bichette were selected lower than might have been expected.
Who will the Tigers take with the first pick in the MLB draft?
Kiley McDaniel breaks down some key storylines for his latest MLB mock draft.
Kurkjian reflects on anniversary of Mike Schmidt's retirement
Tim Kurkjian looks back on Mike Schmidt and how his decision to retire was so sad to the people in baseball.
Tim Kurkjian's Baseball Fix: The day Mike Schmidt realized it was time to go
The season wasn't two months old. But Mike Schmidt had standards. And he wasn't meeting them. So he decided to walk away.
Biggest needs and best fits: 2020 MLB draft guide for all 30 teams
Here's everything you need to know about where your favorite team will be picking and the prospects it should be looking at in this year's unique draft.
On this date: Harper and Strickland square off in heated brawl
On May 29, 2017, Bryce Harper takes exception to getting hit by Giants pitcher Hunter Strickland, and Harper connects on a punch to the face of Strickland. Both players would be ejected.