Aron Gunnarsson gave the visitors the lead in the 23rd minute. Don Cowie's corner was flicked on by Ben Turner, Matthew Connolly knocked the ball down and Gunnarsson was there to sweep home from just yards out.
However, on 72 minutes Cowie was deemed to have upended Mark Little in the box and McCann equalised from the spot.
And seven minutes later, McCann netted another penalty after Dwight Gayle was felled by Connolly.
The victory moves Posh - unbeaten in six games - out of the drop zone and into 21st place, but they are only above Wolves on goals scored.
Robert Olejnik saved well to deny Peter Whittingham early on, but the Peterborough stopper was picking the ball out of the net soon afterwards when the visitors won a corner and the ball broke for Gunnarsson to finish from close range for his eighth goal of the campaign.
Whittingham went close to making it two shortly after but his shot cleared the crossbar as Malky Mackay's men turned the screw and looked for a second.
Peterborough were rattled but two efforts from Lee Tomlin had the home fans believing that all was not lost for their side.
Posh continued to press after the break and after 49 minutes, Joe Newell's cross was met by Kane Ferdinand, whose header brought a fine stop from Marshall.
Ten minutes later, Craig Alcock latched onto a corner from Tomlin but his effort from 12 yards cleared the crossbar.
Posh were asking big questions of Cardiff but when they finally drew level after 72 minutes, it was from the penalty spot.
When Cowie fouled Little in the box, McCann - who had only entered the action two minutes earlier as a 70th-minute replacement for Ferdinand - showed few signs of nerves when he blasted home from the spot.
It was no more than Darren Ferguson's side deserved and it got even better for the hosts seven minutes later when Connolly fouled Gayle in the box and McCann again despatched the penalty.
Cardiff had chances in the closing stages but Peterborough held on to claim a valuable three points.