Scotland beat Australia 15-13 last week and looked good value to pull off another big win after opening up a 10-8 lead at the midway point against the Springboks.
Mapimpi had raced over after some good play from Siya Kolisi for the opening try of the contest after Finn Russell and Elton Jantjies shared a penalty apiece.
But the hosts hit back strongly with a brilliantly worked try that started with Russell chipping a cross-kick left and Stuart Hogg eventually collecting a bouncing ball to run in.
Russell successfully added the extras, unlike Jantjies earlier in the game, but he was wayward with a straightforward penalty just before the interval.
That proved costly as Damian de Allende flicked a great pass to Mapimpi three minutes into the second period to double South Africa's try count.
Jantjies this time made no mistake from the conversion and added six more points from the boot to punish ill-disciplined Scotland, which did manage to restore some hope when Hogg was fed by Duhan van der Merwe for a second try of his own, his 24th for Scotland, tying a record held by Ian Smith and Tony Stanger.
But any hopes Scotland had of a first win over the reigning world champion since 2010 were ended when Handre Pollard (two) and Francois Steyn kicked over a trio of late penalties.