Marvin Morgan struck twice in the first half for the League Two side and strike partner James Collins was on target in between, Ainsworth the provider on all three occasions.
Derby responded through Chris Maguire and Theo Robinson after the interval but Graham Turner's side hung on.
County enjoyed the bulk of the early possession, but without creating any clear chances until Kevin Kilbane went close, and against the run of play Morgan rose highest to power home Ainsworth's corner and put Shrewsbury in front in the 16th minute.
Ainsworth was again the provider in the 34th minute as Collins scrambled home the second from close range, before Steve Davies fired over at the other end.
Nigel Clough's side had no answer to Ainsworth's pace and trickery and the winger, who started his career with two appearances for Derby as a teenager, earned yet another assist three minutes later when his cross was once again finished by Morgan.
Davies tried his luck from distance again and Maguire shot just over, but Morgan went in search of a first-half hat-trick and saw his effort blocked by defender Mark O'Brien.
Straight from the restart after half-time, though, Maguire gave hope to Derby when he headed in Ben Davies' left-wing cross to reduce the arrears - and visiting keeper Ben Smith had to be alert to deny Robinson shortly afterwards.
Nicky Wroe twice threatened for the Shrews and Morgan headed wide before Frankie Fielding punched away Ainsworth's effort.
But Stephen Pearson drew another sharp save from Smith and, after Morgan went close once more, Derby broke from another Ainsworth corner and Tomasz Cywka set up Robinson to cut the deficit to a single goal.
Ben Davies went close to an equaliser and O'Brien was off target from 20 yards as the game ticked into stoppage time, but Shrewsbury held on for a memorable victory.