South Africa made 496 for three before declaring during the tea break.
Opening batsman Elgar and Hashim Amla (137) put on 215 for the second wicket as South Africa again gave Bangladesh cause to regret their decision to send the hosts in to bat.
Amla became the first batsman to fall to a bowler when he slashed a wide ball from Mustafizur Rahman to backward point in the first over after lunch.
The left-handed Elgar, who had gone past his previous highest Test score of 140, looked set for a double century until an attempted pull shot against Shafiul Islam looped off a top edge to short midwicket.
He had batted for more than nine hours and faced 388 balls, with 15 fours and three sixes.
Amla went to his 27th Test century in fluent style. He joined former captain Graeme Smith as the country’s second-highest century-maker, behind only Jacques Kallis, who made 45. Amla scored his runs off 200 balls with 17 fours and a six.
Temba Bavuma (31 not out) and Faf du Plessis (26 not out) added an unbeaten 51 for the fourth wicket but were made to work for their runs as Bangladesh set defensive fields, with their seam bowlers often bowling wide of the stumps.
Download our app and read this and other great stories on the move. Available for Android and iOS.