Jake Cronenworth homered, Wil Myers doubled in two late runs and the Padres recovered after blowing leads of 4-1 and 6-4. Machado finished with three hits, raising his batting average to .359.
''Any offense that's winning ballgames is doing well,'' Myers said. ''Obviously, there's some things that we can improve on. We're hitting well. That's all that matters.''
Darin Ruf homered twice for San Francisco. The Giants have lost four of six.
After the Giants tied the game on Wilmer Flores' two-run single off Luis Garcia with two outs in the ninth, Machado drove a slider on the outside of the plate from Camilo Doval (0-2) deep into center field to knock in Cronenworth, who began the 10th as the automatic runner at second base.
Two batters later, Jurickson Profar added an RBI single that appeared to ricochet off Doval's foot for his third hit of the game, scoring Machado.
''To come back and put two up right away just shows you what this club's been about all year,'' Melvin said. ''It's great to see and we keep developing that personality as the season goes along, but it's not easy to do.''
Profar was booed throughout the game after the left fielder got into an exchange with fans in the third inning.
Garcia (2-2) overcame his struggles in the ninth to win. Robert Suarez pitched the 10th for his first career save. He allowed Brandon Crawford's RBI single with one out but retired the next two batters to end it.
Melvin returned to the Padres less than two weeks after undergoing prostate surgery May 11.
''It went well, that's what I can share with you,'' Melvin said before the game. ''I'm glad I'm back. It's miserable being away. One (game) gives you a little perspective, which is actually kind of good. Any more than that's miserable. Just glad I'm back.''
Ruf hit a two-run homer off Sean Manaea in the third and added a solo drive in the fifth. It was the second multi-homer game of Ruf's career and first with the Giants.
The game was delayed briefly after Profar got into it with fans before the start of the third inning. Profar was warming up in left field and faked as if he was going to throw a ball into the stands, then turned and tossed it into the seats along the third base line. Someone threw the ball back onto the field. At the same time another ball was thrown onto the field, prompting crew chief Ted Barrett to walk over as Profar pointed into the stands. After a few moments, Barrett retrieved one ball and Profar grabbed the other as fans booed before play resumed.
''I tried to throw it to a Padres fan,'' Profar explained. ''I think he missed it and then some Giant fan caught it. When I turned around he threw it back and almost hit me. Then I threw it back and they started throwing more. The next time they threw a beer bottle. These guys are the worst in the league. They can talk but as soon as you try to throw stuff, that's different.''
Melvin said the situation was uncalled for. Myers, however, didn't sound surprised.
''It's pretty hostile here,'' Myers said. ''Every time you come play, the fans are very engaged in the game. I don't agree with what they were doing tonight, but this is who they kind of are. Tough place to play, good environment.''
Manaea remained winless since April 18. The left-hander allowed four runs in six innings. It's the first time in nine career starts against the Giants that Manaea has given up more than two earned runs.
San Francisco starter Jakob Junis, whose only win this season came in relief, allowed four runs and seven hits in six innings.
Padres: LHP Tim Hill was activated off the 10-day injured list after missing 13 games with left shoulder inflammation. RHP Steven Wilson was optioned to Triple-A El Paso.
Giants: OF LaMonte Wade Jr. was placed on the 10-day IL because of left knee inflammation. OF Luis Gonzalez was called up from Triple-A Sacramento.
Padres RHP Joe Musgrove (4-0, 2.20 ERA) has allowed two earned runs or fewer in all seven starts this season. Giants LHP Carlos Rodon (4-2, 3.49) is coming off his worst outing of the season - he gave up eight earned runs in 3 2/3 innings against the Cardinals.
More AP MLB: https://apnews.com/tag/MLB and https://twitter.com/AP-Sports
Copyright 2022 STATS LLC and Associated Press. Any commercial use or distribution without the express written consent of STATS LLC and Associated Press is strictly prohibited.