Paul Terry opened the scoring for the Glovers in the 46th minute, but Shane Tudor stunned Huish Park by firing Cambridge level after 77 minutes.
With just five minutes left, Phil Jevons hit the winner from the penalty spot after Yemi Odubade had been fouled by John Ruddy.
The result at Meadow Lane also gave Swansea a boost, as they moved level on points with United - also thanks to a late penalty.
Lee Trundle's last-minute spot-kick clinched a 3-2 win at Boston.
But, United's top-three position was not affected massively, as Southend could only managed a draw at Mansfield.
Adam Murry had given the Stags the lead, but Mark Gower hit the equaliser after being set up by former Iron target Fredy Eastwood.
The cash-strapped Cods lost out in the Lincolnshire derby to Lincoln, but were boosted by a bumper crowd of 7,091 - their highest of the season. Former postman Simon Yeo hit a hat-trick for the Imps.
Northampton dropped to sixth after losing 2-0 at Bury, whose goals came from Jon Newby and former Iron loanee Andy Keogh.
Trevor Benjamin was sent off for Northampton, who are replaced in fifth by Darlington who cruised to a 3-0 win over Shrewsbury.
Chester finished their goalless draw against Rochdale with nine men, after Paul Carden and Cortez Belle were both sent off. It was Belle's third red card of the season.