The champions gained the fourth batting bonus point they needed to ensure that victory here will guarantee the title, regardless of whether Somerset defeat Notts at Taunton.
Amid unbearable tension, Yorkshire obtained the crucial point for reaching 350 when last man Ryan Sidebottom turned Toby Roland-Jones to the long-leg boundary.
Yorkshire had been 334-9 when Sidebottom arrived at the crease to join Tim Bresnan, who top-scored with a brilliant, undefeated 142.
Yorkshire were finally dismissed on the stroke of tea when Sidebottom was bowled by Ollie Rayner, ending a last-wicket stand of 56 in 22 overs.
Yorkshire resumed on 333-8 after lunch, and Jack Brooks fell to the fifth ball after the interval when he pushed Tim Murtagh to Nick Gubbins at mid-on to fall for a duck.
Sidebottom had a close shave when he edged Steven Finn just short of James Franklin at first slip, and an even closer one when he survived an lbw shout against Roland-Jones with the score on 349.
Although television replays showed that the ball would have hit the stumps, they also revealed that Sidebottom got the faintest of edges on to his pad, and that umpire Rob Bailey had made the right call.
The drama intensified when a combination of bad light and rain then forced the sides off for 68 minutes with the total marooned on 349-9.
Bresnan twice played-and-missed to spinner Ollie Rayner in the first over after the restart, but Yorkshire breathed a sigh of relief when Sidebottom turned Roland-Jones for the crucial boundary.
Had Yorkshire not reached 350, they would have needed Notts to hold out for a draw or beat Somerset, having been set 541 for victory.
Yorkshire started the day on 235-6, needing 115 from a minimum of 41 overs to claim that crucial fourth batting point.
They rode their luck in the early stages as Rafiq was dropped twice before the second new ball became available after 11 overs.
After twice edging Steven Finn over the slips to the third man boundary, Rafiq, who began the day on 20, was dropped at third man by Nick Compton off Finn on 33.
He was then spilled on 38 at second slip by Ollie Rayner off James Franklin as Middlesex’s fielding again let them down.
Rafiq went on to reach his half-century from 78 balls with nine fours before Bresnan progressed to his sixth first-class hundred.
Bresnan, who started the day on 72, got to the landmark from 222 balls with 10 fours after just over five hours at the crease.
It took a ball change and a misjudgement from Rafiq to end the seventh-wicket stand 25 minutes before lunch.
Tim Murtagh, operating from the Nursery End, bowled Rafiq through the gate to end a 97-ball innings that included 10 fours and a six.
Bresnan was joined by Steve Patterson, who scored 11 before edging Finn to Rayner at second slip.
It was desperately close to being a no-ball, but the wicket was upheld by the third umpire.