A last-gasp penalty from Daniel Johnson sent Preston into the third round of the Carabao Cup with a 2-1 victory against 10-man Derby at Pride Park.
Derby had led through Jason Knight's goal but after Mike te Wierik was sent off, a brilliant strike from Tom Barkhuizen levelled the scores with 11 minutes remaining.
With the game seemingly heading for a penalty shoot-out, referee Andy Davies ruled Lee Buchanan handled a cross and Johnson made no mistake to set up a home tie with Brighton or Portsmouth.
Wayne Rooney had made his first start of the season as one of four Derby changes, while Preston made 10 following Saturday's home loss to Swansea.
Rooney played as the main striker as Derby looked for their first goal of the new campaign but after a shapeless opening, it was Preston who fashioned the first chance.
Alan Browne's flighted pass in the 24th minute found Sean Maguire on the edge of the six-yard box but he scuffed his shot under pressure from Matt Clarke.
Preston had another good chance four minutes later when the ball dropped invitingly for Jayden Stockley but he fired well over and after Clarke lost possession, Josh Harrop's 20-yard drive was beaten out by Kelle Roos.
Derby created nothing until the 42nd minute when Buchanan got the ball on the edge of the area but his shot was too high to trouble Declan Rudd.
The game needed a goal and Derby supplied it in the 51st minute with Graeme Shinnie pulling the ball back from the left and after Knight's first shot was blocked, he followed up to bundle in the rebound.
But four minutes later, the hosts were reduced to 10 men when Dutch centre back te Wierik pulled over Maguire, who would have been through on goal, just outside the box and was shown a straight red card.
Paul Gallagher's free-kick came back off the wall but Preston sensed there was now an opening and Roos had to react quickly in the 72nd minute to keep out Brown's deflected shot.
But Roos could do nothing in the 79th minute when Barkhuizen found space just outside the area and unleashed a shot from 20 yards that flew into the top left corner.
Barkhuizen had another goalbound effort blocked by Clarke but Preston won it in added time when Buchanan handled as the ball came into the box and Johnson coolly sent Roos the wrong way to settle the tie.