The Mets had grabbed the lead with a four-run rally of their own in the eighth, but New York closer Bobby Parnell (3-6) couldn't hold the lead and the Cubs were aided by a David Wright error in the ninth.
The Mets third baseman opened the frame by misplaying Geovany Soto's grounder. Pinch-hitter Bryan LaHair then delivered double, sending pinch-runner Lou Montanez to third.
Ramirez followed by grounding his decisive two-run hit through the right side and Carlos Marmol survived a shaky bottom of the ninth to pick up his 34th save.
The victory went to Kerry Wood (3-5), despite allowing the Mets to seize the lead in the eighth on Jason Bay's two-out, two-run single.
Marlon Byrd added an RBI double for the Cubs, who evened their three-game set against the Mets at one win apiece.
Celebrity Breakups and divorces of 2014 [PHOTOS]