San Francisco had a baserunner aboard in every inning against White Sox starter Gavin Floyd (4-2), who withstood each threat and bounced back from a May 11 loss at Seattle in which he lasted only 3 2-3 innings.
The right-hander allowed four hits over six innings and didn't give up an earned run for the fourth time in eight outings this year. He struck out three and walked three.
Four relievers finished the five-hitter for Chicago's fourth shutout, with Bobby Jenks getting three outs for his ninth save in 11 chances. The Giants were blanked for the third time.
Ramirez, a rookie who defected from Cuba last November and has the nickname "Cuban Missile," had only three hits in his previous 27 at-bats (.111) before connecting off Giants starter Jonathan Sanchez (2-3) with two outs in the seventh and Nick Swisher aboard on a single.
The ball, which landed in the left-field bleachers, was thrown back onto the field before a catcher in the Giants' bullpen tossed it into the seats nearby. Then, White Sox third base coach Jeff Cox walked over and retrieved the ball for Ramirez.
The teams met for the second time since interleague play began in 1997. The Giants took two of three in Chicago from June 10-12, 2003.
White Sox manager Ozzie Guillen and Giants skipper Bruce Bochy were winter ball teammates in Guillen's native Venezuela.
San Francisco's Randy Winn hit his 300th career double in the fifth for an 11-game hitting streak. He has hit safely in 13 of 14 games in May and is batting .373 (22-for-59) for the month. Ray Durham had a double among his three hits but the Giants missed opportunities and lost for the 10th time in 14 games.
Sanchez's winless stretch reached four starts. The left-hander hasn't won since beating Cincinnati on April 25.
A.J. Pierzynski, the Giants' catcher in 2004 before joining the White Sox, was booed at every opportunity in his first trip back to San Francisco's waterfront ballpark since he played here.
Pierzynski tripled in the sixth for his first triple since Aug. 22, 2004, while with the Giants against the New York Mets. It was his third in San Francisco.