Mahrez blasted in from 20 yards with the last kick of the match to send the Desert Foxes through to their first final since lifting the trophy for the only time in 1990.
Mahrez (right) vies for the ball with Nigeria's Ahmed Musa during the 2019 Africa Cup of Nations (CAN) semi-final football match. AFP photo
Riyad Mahrez rifled in a stoppage-time free-kick to earn Algeria a dramatic 2-1 victory over three-time champions Nigeria on Sunday and set up a rematch with Senegal in the Africa Cup of Nations final.
Algeria led through a first-half own goal by William Troost-Ekong before Nigeria equalised with an Odion Ighalo penalty on 72 minutes awarded after a VAR review.
However, as extra time looked necessary, Mahrez blasted in from 20 yards with the last kick of the match to send the Desert Foxes through to their first final since lifting the trophy for the only time in 1990.
Algeria will take on Sadio Mane's Senegal for the title on July 19, in a repeat of a group stage clash they won 1-0.
"We are very happy to be in the final of the Africa Cup of Nations because it is something very special," said Mahrez.
"It was our goal to do it for the people and for our families. We know they are behind us all the way. It's my most important goal for the national dream. Yes, it's my dream to win it and hopefully we will be able to."
Nigeria coach Gernot Rohr conceded Algeria were worthy winners after denying the Super Eagles the chance to compete in an eighth final.
"I have to congratulate the players for a big fight against this very good team. They won it in the last minute and they deserved it," said Rohr. "A wonderful free-kick made the difference."
An injury to Youcef Atal, who fractured his collarbone during the quarter-final win over Ivory Coast on penalties, forced Djamel Belmadi to start Mehdi Zeffane at right-back, while Nigeria fielded the same team that overcame South Africa here.
Algeria supporters filled most of the lower tier at one end of the ground, dwarfing a small section of Nigeria fans, but there were large swathes of empty seats at the 75,000-capacity Cairo International Stadium.
Mahrez leads by example
Leading scorers in the tournament and considered the most impressive side, Algeria quickly asserted themselves as Ramy Bensebaini headed narrowly over before a wicked Youcef Belaili cross nearly lead to a goal for Baghdad Bounedjah.
Hesitant defending from Kenneth Omeruo just outside his own area then allowed Bounedjah to nip in, but the Al-Sadd striker could only shoot tamely at Daniel Akpeyi.
Seemingly having moved on from the penalty miss that reduced him to tears in the last round, Bounedjah was consistently proving a nuisance to a nervy Nigeria backline with Jamilu Collins relieved to see a toed clearance land on the roof of the net.
Nigeria countered intermittently as Ighalo dragged wide after Mahrez was caught in possession, but the Manchester City winger promptly atoned as Algeria made the breakthrough on 40 minutes.
After gathering the ball on the right Mahrez danced past Collins before sending over a cross that flicked off the left-back and struck a helpless Troost-Ekong in the midriff before tricking over the line.
Rohr has regularly praised his side's resilience, particularly the way they bounced back from a shock loss to Madagascar to eliminate defending champions Cameroon, and Nigeria again hit back with the aid of VAR.
Gambian referee Bakary Gassama initially dismissed Nigerian penalty appeals when a Peter Etebo shot struck the arm of Aissa Mandi but reversed that decision, with Ighalo sending Rais Mbohli the wrong way from the spot for his competition-best fourth goal.
Sofiane Feghouli blazed over late on and extra time beckoned after Ismael Bennacer rattled the bar in stoppage time, but Mahrez incredibly won it in the 95th minute with a thunderous strike into the top corner.