San Francisco stopped a skid that matched its longest of the season, and avoided the club's first winless homestand of at least seven games in the 15-year history of AT&T Park.
The Pirates, who had won three in a row, led 5-4 in the sixth when Chris Stewart drew a one-out walk with runners on second and third.
Machi (6-0) got the ball back and noticed Travis Snider had left second base, apparently thinking it was a bases-loaded walk. Machi threw to shortstop Brandon Crawford to trap Snider. Crawford returned the ball to Machi, who fired to third baseman Pablo Sandoval to get Gaby Sanchez as he tried to go home.