But he was twice put down before lunch off Udal, by Scott Newman and Sam Robson.
But both fell to Udal, who finished the innings with a season's best 6-36.
Only Ervine and Udal stopped the rot, setting up the game's thrilling finale.
BBC: SPORT | Cricket | Counties | Hampshire secure final-ball tie
Thirty-six-year-old Shaun Udal, who made his debut in Pakistan, took just three wickets in as many Tests.
BBC: SPORT | Cricket | England | Giles worry over arthritis in hip
But when Justin Langer caught Udal and Steffan Jones bowled Ervine they were four short with three balls to go.
BBC: SPORT | Cricket | Counties | Hampshire secure final-ball tie
And Tim Murtagh's eight-over new-ball spell of 2-19 began to vindicate Udal's decision to declare 91 runs behind.
And, in any case, Udal's frustration at those misses was offset considerably by the spirit his side showed.
But they collapsed from 70-2 to 82-8 as the middle order struggled against spinners Shaun Udal and Murali Kartik.
Udal, 40, will commit to his job at Lord's for the entire summer and reject a lucrative short-term deal in India.
Udal ended up without a wicket, while England hopeful Steven Finn did little to boost his Test hopes, finishing with 2-81.
Shah, who resumed on 50 after retiring with cramp at tea on day one, was batting with Shaun Udal when the middle session began.
BBC: SPORT | Cricket | England | England put India under pressure
Derbyshire struggled against Shaun Udal (4-37) and Pedro Collins (4-46), with only Chris Rogers (75) resisting as they were all out for 182.
Chasing 221 to win the home side slid to 148-7 before Sean Ervine and Shaun Udal put on 61 for the eighth wicket and gave Hampshire hope.
BBC: SPORT | Cricket | Counties | Hampshire secure final-ball tie
Udal played well for his unbeaten 36 but India will be happy with a win and, more meaningfully, some solid practice before the first Test.
However, Tremlett's fireworks were in vain as Pedro Collins removed Stewart Walters and Tim Linley while Shaun Udal saw off Jade Dernbach to secure a win.
With the game looking set for a draw, Udal declared on 414-8 after Dawid Malan had made 88, then succeeded in putting the Welsh under real pressure.
Udal who was sent out as night-watchman and he might have departed second ball after a huge appeal for caught behind, but umpire Hair did not agree.
BBC: It's been a long time since I've played. I'm really pleased
Sir Peter said he did not think the courts would take Udal Law "terribly seriously" and declared that at his age he would consider prison a new experience.
BBC: NEWS | UK | Scotland | Police swan find hits wrong note
The pair had put on 32 for the seventh wicket when India struck again, Patel finally getting an overdue wicket when Udal played all around a full straight delivery to be lbw.
BBC: SPORT | Cricket | England | England put India under pressure
In reply, Rogers went on to make his highest Championship score since June but his was the high point among more poor batting as Derbyshire perished to Udal's spin and Collins' pace.
The Division One leaders resumed play on 410 for five, and centurion Ali Brown was the first batsman to fall when he was removed by Shaun Udal, who took four wickets, for 135.
Udal survived a catch appeal being ruled out and also being dropped early on to get Hampshire's innings off to a good start, and when Michael Carberry's 65 pushed them to 124-2, they looked in control.
BBC: SPORT | Cricket | Counties | Hampshire secure final-ball tie
Dexter, having swept Shaun Udal for a towering six, was the eighth wicket to fall when he was caught at deep cover by Kartik off Tyron Henderson, and Essex's total never looked like being enough.
There are also places for 40-year-old Middlesex skipper Shaun Udal and Notts all-rounder Samit Patel, who was recently left out of the squad for the Caribbean after "failing to reach acceptable standards of fitness for international cricket".
Shaun Udal had declared Middlesex's innings at 258-3 late on the the third day, but the Wantage Road wicket continued to play favourably for batsmen, and former Essex pro Peters played the innings of his career on Saturday.
Tremlett kept up the big hitting but Udal removed Dernbach for nine and Collins dismissed Linley without scoring in the following over to wrap up a big win for Middlesex, only their second victory over their rivals since 1995.
应用推荐