South Africa, who scored 361 in the first innings and bowled Sri Lanka out for 321, batted through a closely-fought third day to close on 257 for seven.
Gibbs, under pressure after three cheap dismissals in the series, batted positively in the morning and then grafted hard during the afternoon as Sri Lanka bowled tightly to defensive fields.
The 32-year-old right-hander stroked 11 boundaries and one six during a 190-ball innings before missing out of what could have been his first test hundred for 19 months and the 15th of his career.
Nevertheless, it was a timely return to form for a batsman who had passed 20 only once in his previous 11 innings.
The Sri Lankans were kept in the game by prolific off spinner Muttiah Muralitharan who snared three wickets in the final session to finish with four for 86, one short of a record-equalling fourth consecutive 10-wicket match haul.
South Africa had dominated the morning, adding 100 runs from 29 overs.
The only casualty of the session was makeshift opener Andrew Hall, who was caught behind for 32 off Farveez Maharoof.
Hall and Gibbs shared an opening stand of 76 runs, allowing South Africa to tighten their grip on the game after their bowlers stole the initiative on day two.
The tension mounted in the afternoon as Sri Lanka applied pressure with two run outs, the first a controversial decision by television umpire Tyrone Wijewardene who ruled Jacques Rudolph out for 15.
Rudolph was slow to leave the field, convinced Chamara Kapugedera's hand had brushed the boundary rope before he threw down the stumps with a direct hit.
Kapugedera was then responsible for running out Hashim Amla (8), who was sent back too late by Gibbs, with an accurate throw from short fine leg.
Gibbs fell on the stroke of tea after top-edging a sweep off Muttiah Muralitharan, lifting the morale of a previously frustrated Sri Lankan team.
After tea the flow of runs reduced to a trickle as Maharoof found some reverse swing with the old ball and Muralitharan plugged away accurately.
However, Ashwell Prince (17) and AB de Villiers (33) chiselled out 41 crucial runs for the fifth wicket.
The game turned again in the final hour as Muralitharan struck twice in an over, snapping up a return catch off Prince and then the fortuitous scalp of de Villiers, who edged onto his helmet to be caught at short leg.
Shaun Pollock (14) perished shortly afterwards after picking out Upul Tharanga at square leg with a pull stroke.
Sri Lanka won the first test in the two-match series by an innings and 153 runs.