Chasing 131 for victory, Pakistan slumped to 79-4 after skipper Babar Azam fell to left-arm spinner Prabath Jayasuriya, who claimed 4-56 in the second innings.
Imam combined with Saud Shakeel to steady the ship but Sri Lanka refused to throw in the towel despite not having a meagre total to defend.
Shakeel, who made a career-best 208 not out in the first innings, fell for 30 and Sarfaraz Ahmed (one) didn't last long either.
But Imam remained unbeaten on 50 and watched from the other end as Agha Salman secured victory in style by hitting the first ball he faced, from Jayasuriya, over the mid-off boundary.
Shakeel, whose maiden Test double hundred helped Pakistan to a decisive first-innings lead of 149, was named Player of the Match.
"The way Saud and Agha played, it totally changed the game," Babar said referring to their 177-run partnership, which helped Pakistan post 461 in the first innings.
"It's not easy to bat in Galle, but the way Saud's playing is outstanding."
Sri Lanka's top order couldn't do justice to their decision to bat first, although the home side reached 312 in their first innings courtesy Dhananjaya de Silva's gallant 122.
"We needed to see off that opening spell but we lost a few wickets," home captain Dimuth Karunaratne said.
"Overall, the bowlers did well, but we dropped a few catches and that cost us. We have to assess what went wrong and come back again."
The teams move to Colombo for the second and final Test beginning on Monday.