At tea England were 155 for six needing a further 128 runs to reach their victory target of 283.
Jonny Bairstow (28 not out) and all-rounder Chris Woakes (five not out) -- who had already taken 11 wickets in this match -- were still at the crease but with just the tail to come. The most England had ever made before to win in the fourth innings of a Lord's Test was 282 for three against New Zealand in 2004.
England were making steady progress at 135 for four when leg-spinner Shah, who took six for 72 in the first innings, struck Sunday. A ball after Gary Ballance had pulled him for four, Shah bowled the recalled left-hander behind his legs for 43 with a delivery that spun out of the rough.
Moeen Ali (two) then charged down the pitch in an attempt to drive Shah, only to be bowled between bat and pad by a well-flighted delivery. Shah had taken two wickets for two runs in six balls and England were 139 for six.
More From This Section
England captain Alastair Cook launched the chase with a four off the very first ball of the innings when he cut a loose Mohammad Amir delivery to the boundary.
All the pre-match focus had been on Amir, making his return to Test cricket at the scene of his 2010 spot-fixing crime. But it was fellow left-arm quick Rahat who stunned England.
His back-off-a-length ball had left-hander Cook (eight) edging through to wicket-keeper Sarfraz Ahmed. Cook's opening partner Alex Hales made a brisk 16 before carelessly chasing a wide ball from Rahat outside off stump, with Mohammad Hafeez holding a good catch at first slip.
But the star batsman fell into a hooking trap on nine when he paddled a Rahat short ball to Shah, set back at deep square leg. James Vince too got off the mark with a cover-driven four off Rahat.
Vince was on nine when his edged drive off Rahat flew to Younis Khan only for the second slip to drop both the initial low catch and the rebound. Vince struck nine fours, mainly through the offside, on his way to a Test-best 41 not out at lunch without looking assured.
Earlier, Pakistan added just one run to their overnight 214 for eight. Pace bowler Woakes led England off the field after taking five for 32 for a match haul of 11 for 102.