Younis was caught behind off the bowling of Vernon Philander for 111 shortly before stumps, but Shafiq was unbeaten on 111 at the close of play.
Capetown: Centuries by Younis Khan and Asad Shafiq transformed Pakistan's fortunes on the first day of the second Test against South Africa at Newlands on Thursday.
Pakistan were 253 for five at stumps, having been reeling at 33 for four after being sent in.
Younis was caught behind off Vernon Philander 13 balls before the close for 111, while Shafiq was unbeaten, also on 111.
The pair put on 219, a record partnership for any wicket by Pakistan against South Africa.
It was a remarkable turnaround after the South African fast bowlers scythed through their top order early in the day.
Pakistan seemed in danger of another humiliation following their dismissal for a record low of 49 on the way to a 211-run defeat in the first Test in Johannesburg.
Graeme Smith's decision to bowl was prompted by cloudy weather, which followed early morning rain.
It seemed fully justified as Morne Morkel took two wickets and Philander and Dale Steyn claimed one each, with none of the batsmen looking comfortable.
Younis, Pakistan's most experienced batsman, had to survive some challenging deliveries from Steyn at the start of his innings, while Shafiq followed a fighting half-century in the second innings in Johannesburg with an innings which grew in quality the longer it progressed.
It was slow going at times, with Younis and Shafiq adding only 14 runs in the first 13 overs after lunch.
The 50 partnership was posted off 151 balls, but their second 50 was scored off 88 deliveries and the pair scored freely in the afternoon before the second new ball was due.
Younis reached his 21st Test century -- and his fourth against South Africa -- after facing 192 balls and hitting six fours and three sixes, all of the sixes coming off left-arm spinner Robin Peterson.
The stocky Shafiq followed in the last over with the old ball, notching his third Test hundred off 201 balls with 13 fours and a six.
Steyn appeared to have made a breakthrough with his third delivery with the new ball when Younis, on 105, was adjudged leg before wicket by umpire Steve Davis.
But the batsman immediately called for a review which showed that the ball flicked the inside edge of his bat before crashing into his front pad.
Younis then fell victim to a successful review by South Africa after being given not out by umpire Bruce Oxenford. Replays showed he had been caught behind off an inside edge and his thigh pad.
The stand lasted for 439 balls and gave Pakistan heart after a poor start.
Opening batsmen Mohammad Hafeez and Nasir Jamshed started cautiously, scoring just ten runs before the left-handed Jamshed was caught behind off Philander for three.
He had survived a chance to Faf du Plessis at gully off the previous delivery.
Hafeez edged Steyn to first slip after making 17 before the tall Morkel took two wickets in an over, having Azhar Ali caught behind attempting an extravagant drive before Pakistan captain Misbah-ul-Haq was caught at short leg off a sharply rising delivery.
|Director says 'cut' on Sreesanth's guest appearance No cricket connection for this film.|
|PETA wants spot-fixing accused in its ad Says trio can generate "public goodwill" by featuring in campaign promoting sterilisation of dogs and cats.|
|Woods, Garcia but surely no fried chicken Tiger Woods was hurt by a comment made by Sergio Garcia who later apologised.|
|In China, badminton becomes bird flu's latest vicitm Shuttlecocks are made from bird feathers, making them a risk.|
|Mumbai-born tycoon buys Sacramento Kings Vivek Ranadive will now have the best seats in the house!|