After being comfortably beaten in the first two games, Markram led his side to victory with an unbeaten 69 to secure a comfortable victory for the tourists.
Afghanistan's innings was dominated by opener Rahmanullah Gurbaz who struck 89 runs from 94 deliveries as the hosts' batting order tumbled around him.
South Africa capitalised on their chances through the middle phase, with Rahmat Shah (one), Hashmatullah Shahidi (10) and Ikram Alikhil (four) all run out.
Nqaba Peter (2-22) claimed the final wicket of the day after capturing Naveed Zadran (one), with South Africa set a target of 170 to secure their consolation victory.
However, the Proteas started poorly in their run chase, losing the wickets of Temba Bavuma (22), Tony de Zorzi (26) and Reeza Hendricks (18) for just 80 runs.
Markram's innings steadied the ship for the Proteas, with his unbeaten knock coupled with a 90-run partnership with Tristan Stubbs (26) seeing the tourists home.
The victory salvaged some pride for South Africa, having been well-beaten in the first two encounters, while also providing some momentum heading into their upcoming series against Ireland.
Data Debrief: Markram restores Proteas' pride
Markram produced an impressive display at Sharjah Cricket Stadium, with his unbeaten 69 including three sixes during his 98-minute spell.
He also ended his innings with an impressive strike rate of 102.99, a total only bettered by AM Ghanzanfar for the hosts (206.67).