The 24-year-old opener scored a patient 148 not out as the tourists reached 337-4 in their second innings at stumps on the last day while chasing an improbable 458 runs.
Pakistan made no serious attempt to reach 458, which would have smashed the world-record run chase of 418 by the West Indies against Australia at Antigua in 2003.
Malik showed the way in a marathon, 369-ball innings as the visitors batted to save the match rather than win on a fifth-day pitch.
Malik, 30 overnight, impressed with his temperament and shot-selection as he batted more than a day to thwart Sri Lanka. He hit two sixes and 21 fours in his 369-ball innings.
"Malik played a fantastic innings under pressure, but the pitch was good to bat on," said Sri Lankan vice-captain Kumar Sangakkara, who was named man of the match for his second-innings 185.
The Pakistani opener, whose previous Test-best was 64 against the West Indies at Jamaica last year, completed his hundred in style when he drove spinner Sanath Jayasuriya for a four in the last session.
Malik put Pakistan on the right track, adding 115 for the third wicket with Faisal Iaqbal (60) and 81 for the next with skipper Inzamam-ul-Haq (48).
"Malik played very well and took the pressure off us. It is good that junior players also contributed. It was a team effort," said Inzamam.
Sri Lanka's hopes of winning the match depended on ace off-spinner Muttiah Muralitharan, the only bowler to capture 1,000 Test and One-day international wickets.
But the spinner had a rare off-day, claiming just one wicket in 28 overs.
When Muralitharan struck, Pakistan had virtually saved the match. His lone success came in the last session when he had Inzamam caught bat-pad by Tillakaratne Dilshan at silly-point.
Pakistan began impressively when they denied the hosts success in the rain-curtailed morning session after resuming at 89-2, thanks to Malik and Faisal.
With Muralitharan going wicketless in the first session, Sri Lanka took the second new ball immediately after it was due in the afternoon session and tasted instant success.
Seamer Farveez Maharoof provided the breakthrough in his first over when he trapped Faisal leg-before, but not before the batsman had frustrated the hosts for more than a session.
Faisal, nephew of former Pakistan captain Javed Miandad, hit six fours in his fourth Test half-century.
Sri Lanka continued to struggle for wickets as Malik and Inzamam batted steadily. The Pakistani captain hit eight fours in his 89-ball knock.
All-rounder Abdul Razzaq made merry in pressure-free conditions late on, hitting four boundaries in his unbeaten 31-ball 20.
The second and final Test begins at Kandy on April 3.