Elgar's century allowed the home side to reach stumps on a solid 297-6 after it had lost the toss and been asked to bat on an overcast and muggy morning that offered plenty of assistance to Sri Lanka's seamers.
The left-hander remained steadfast against the swinging ball early on, before scoring more freely when the sun came out later in the day and the pitch improved.
Although Elgar lost opening partner Stephen Cook to the fourth ball of the day, he shared in a 66-run stand for the second wicket with Hashim Amla (29), then added 76 with captain Faf du Plessis (38) for the fourth wicket.
The South African captain had already been dropped off the bowling of Rangana Herath when he played an expansive drive at a turning delivery and was caught at slip.
More From This Section
When Temba Bavuma (10) was done for pace by Kumara's bouncer, South Africa was 169-5 and in danger of being dismissed cheaply.
Instead Quinton de Kock played a typically aggressive knock, scoring at close to a run-a-ball in the early stages. Elgar also began to score more freely in the lead-up to his century, and the pair went on to record a partnership of 103 in less than 24 overs.