Australia then lost opener Shaun Marsh as the visitors ended the day on 47-1 and facing an uphill battle to avoid defeat after Pakistan declared their second innings on 400-9.
Paceman Mir Hamza sent back Marsh for four before Aaron Finch (24) and Travis Head (17) steadied the ship, although Australia's hopes of a first series win in Asia since 2011 were hanging by the slenderest of threads with 491 runs still needed.
Azam hit six fours and three sixes in his 171-ball knock before falling leg-before to Mitchell Marsh as he closed in on a deserved century. He put on a 133-run stand with skipper Sarfraz Ahmed (81) for the sixth wicket.
Ahmed also failed to convert his aggressive knock into a century, as he was dismissed leg-before while looking to sweep spinner Marnus Labuschagne.
Earlier, Asad Shafiq and Azam provided stability in the middle order after Pakistan lost a couple of early wickets as the hosts stretched their lead to 369 at lunch.
Resuming the day on 144-2, Pakistan lost the wicket of Haris Sohail (17) when he was stumped by visiting captain Tim Paine off a tossed up delivery from off-spinner Nathan Lyon.
Sohail's overnight partner Azhar Ali (64) was then run out in a bizarre fashion when he edged a Peter Siddle delivery fine of gully and walked up the pitch for a chat with partner Shafiq, thinking the ball was running safely to the boundary.
Mitchell Starc, who is nursing a tight hamstring, picked up the ball that had ground to a halt just inside the rope before sending a throw for wicketkeeper Paine to whip off the bails with both batsman standing idly in the middle of the strip.
Australia were dismissed for 145 on day two, handing Pakistan a 137-run first innings advantage.