The tourists achieved the win in the final session of the fifth day’s play to take a 1-0 lead in the two-match series.
The previous highest successful fourth-innings chase at Galle was 268 by Sri Lanka against New Zealand in 2019.
Left-arm spinner Prabath Jayasuriya took four wickets for the hosts and is on the brink of an unprecedented feat of four straight five-wicket hauls in his first two Tests.
He took 12 wickets on debut in a series-levelling Test win over Australia last week at the same venue, six in each innings.
Pakistan resumed the day on 222-3 and Sri Lanka soon used up their remaining two umpire reviews, with Jayasuriya involved in both calls.
Mohammad Rizwan opened his day’s account with a boundary off Ramesh Mendis and kept up his positive intent by taking singles and twos before he fell lbw to Jayasuriya for 40.
A review did not save Rizwan, a wicketkeeper-batsman who has come into the two-match series after a successful stint with English county Sussex.
Shafique, who recorded his second Test century on day four to lead the chase, has stood firm and played the anchor’s role to perfection so far.