Pakistan lost their last two wickets after the dinner-break to be bowled out for 239 in their second innings, having been called to follow on by Tim Paine’s side.
Nathan Lyon finished with innings best figures of 5-69.
At the dinner break in Adelaide, Pakistan were 229 for eight — still needing to score another 58 to make the hosts bat again.
Pakistan resumed day four on a precarious 39 for three with Masood not out 14 and Shafiq on eight after being bowled out for 302 in their first innings and forced to follow on.
It followed Australia’s mammoth 589 for three declared, built around David Warner’s exceptional 335 not out — the 10th highest Test score of all time.
Needing 248 to make Australia bat again and avoid a second successive innings defeat, they made a decent effort to reach the milestone. Mohammad Rizwan, in only his third Test, was not out 40. Masood made 68 and Shafiq 57.
While Lyon extracted some life out of the pitch, it offered little for Australia’s fast bowlers who were made to toil.
They will be able to take the new pink ball shortly and with the lights coming on, it should offer them more conducive conditions to finish the job.
Masood, who models himself on England’s Alastair Cook, played some glorious strokes, including a six off Lyon, as he assumed a leadership role after Babar Azam and captain Azhar Ali fell cheaply on Sunday evening.
His sixth Test 50, in his 17th Test, arrived with a boundary off Lyon, his sixth four, before he chipped the spinner to mid-off where Mitchell Starc collected an easy catch.
Shafiq, who has 12 Test hundreds to his name, proved to be Masood’s equal and brought up his 25th half-century with a single off Josh Hazlewood.
But Lyon struck again, with Shafiq getting a thin edge to a flighted ball that carried to Warner at slip, before he then removed Iftikhar Ahmed, caught at short leg by Marnus Labuschagne for a battling 27.
Surprise first-innings century-maker Yasir Shah could not repeat the heroics and was Lyon’s next victim, out lbw for 13 after a review.
Shaheen Afridi became his fifth, caught at mid-off by Hazlewood on the last ball before dinner, with Australia on the cusp of inflicting another heavy loss on Pakistan after their innings and five runs defeat in the first of the two-Test series at Brisbane.
Australia won the first Test in Brisbane by an innings and five runs. Pakistan have been defeated in 13 consecutive Tests on Australian shores.