Andre Ethier coaxed a two-out walk from Sergio Romo (3-2) in the 10th and Ramirez followed by blasting a pitch into the left-field stands -- his first round-tripper for Los Angeles since he was obtained in a trade with Miami this week.
The blast made a winner of Shawn Tolleson (1-0), who tossed a scoreless bottom of the ninth. Kenley Jansen racked up his 20th despite yielding two hits in the 10th.
James Loney added two hits and one RBI for the second-place Dodgers, who broke a three-game losing streak, took the first of a three-game series and pulled to within two games of the National League West-leading Giants.
Brandon Belt (3-for-5) forced extra innings with a game-tying, two-run single in the eighth for San Francisco.
Melky Cabrera added three hits and scored a run for the Giants.
Swim Week Miami Beach 2014 [PHOTOS]