The hosts were 256 for one at tea. Elgar made 113 and Markram was unbeaten on 135.
Markram's maiden century came in his second Test, eight days after he was run out for 97 in his debut innings during the first Test in Potchefstroom.
For the second successive match, the South African opening pair gave Bangladesh captain Mushfiqur Rahim cause to regret his decision to bowl first. Their partnership of 243 exceeded the 196 they put on to set up South Africa's 333-run win in the first Test.
Neither batsman gave a chance until Elgar, on 110, gloved an attempted hook off Mustafizur Rahman and wicketkeeper Liton Das leapt high and got the tips of his right glove to the ball but could not hold on.
Also Read
Mustafizur was replaced by Subashis Roy, and when Elgar hooked again, Mustafizur ran in from fine leg to take an excellent diving catch for Bangladesh's only success.
The left-handed Elgar, with a reputation for solid accumulation rather than flashy strokes, hurried to his half- century off 59 balls and his century off 116 deliveries.
Markram was more subdued at the start of the innings but reached fifty off 81 balls. He square drove Rubel Hossain for four to reach 97 and scarcely had time to remember his dismissal on the same score in Potchefstroom before he hooked the next delivery for his 16th four to reach his century off 141 balls.
Disclaimer: No Business Standard Journalist was involved in creation of this content