The Dodgers trailed 1-0 in the eighth when Ramirez came on to pinch-hit, after he was forced to sit on the bench for a second successive game with a calf injury, and sent a pitch from reliever Sergio Romo over the left field wall.
Ramirez then drove in DeWitt with his homer, which was the 548th of his career that tied him with Mike Schmidt for 14th place on the all-time list.
"It's nice to have weapons like Anderson and Manny," Dodgers manager Joe Torre told reporters. "It gives this ballclub the opportunity to come back in games.
"I would not like to see Anderson and Manny coming off the bench in a close game."
Juan Uribe gave the Giants (8-4) the lead with a solo homer in the seventh off 22-year-old starter Clayton Kershaw, who struck out nine in seven innings.
Los Angeles preserved the win in the eighth where three relievers combined to end a bases-loaded threat when Uribe hit into a ground out. Jonathan Broxton then pitched a perfect ninth and recorded the save for Los Angeles (6-6).
Dodgers outfielder Andre Ethier went 2-for-3 and was the only player with multiple hits.
(Reporting by Jahmal Corner in Los Angeles; Editing by Greg Stutchbury)