Markram takes over from regular captain Faf du Plessis, who suffered a broken right index finger during India's six-wicket win in the first match in Durban on Thursday.
Markram will be South Africa's youngest captain since Graeme Smith was appointed on a long-term basis at the age of 22 in 2003.
Convener of selectors Linda Zondi said Markram's appointment was in line with a policy to develop young leaders in the squad.
Markram has been identified as a potential future captain since he led South Africa to victory at the 2014 Under-19 World Cup.
He has captained South Africa A and made his full international debut during the current season, scoring 97, 15, 143 and 125 in his first four Test innings against Bangladesh and Zimbabwe.
He found the going tougher in the recent Tests against India, scoring 140 runs at an average of 23.33. He has only played in two one-day internationals, including the Durban match.
Markram said there was a good team culture and that he would be able to call on the advice of senior players.
"Faf has taken me under his wing since I have been involved in this environment," he said. "We have spoken a lot and this morning at practice he was next to me and chatting about plans."