It is the highest target ever at Galle and Pakistan’s second-highest chase in Test matches.
On day five, the visitors were 222-3 and still needed 120 runs to win. They were cruising toward their goal when they lost three wickets quickly, dropping to 303-6.
However, Shafique, 22, kept his composure during a rain delay to score the winning runs.
After the hosts twice removed Shafique from the game during the morning session, Sri Lanka was left to lament squandered opportunities.
Pakistan’s most fantastic Test chase was 377, which was also against Sri Lanka in Kandy in 2015.
Shafique has two tons in six Test outings and said, “The idea was clear — we had to go for the runs.
The pitch was challenging, but it became simpler as you put more effort into it.
With a 218-run first innings score, Pakistan’s skipper Babar Azam ensured his team was just four runs behind.
Dinesh Chandimal of Sri Lanka then set up a record target in Galle by scoring an unbeaten 94 in his team’s 337 after scoring 76 in the first innings.
With the help of Babar’s 55 and Mohammad Rizwan’s important 40, Shafique led the chase admirably, needing just 408 balls for his 160.
With the loss of Rizwan to spinner Prabath Jayasuriya, Pakistan reached 344-6. Shafique had substantial assistance from number eight Mohammad Nawaz, who scored 19 runs without being out.
The second Test, also at Galle, will begin on Sunday.