With the match heading for extra time, Manchester City forward Mahrez whipped a fierce set-piece into the top corner.
A William Troost-Ekong own goal had given Algeria the lead before Odion Ighalo’s penalty levelled the scores.
Algeria will face Senegal, who earlier beat Tunisia thanks to an extra-time own goal, in the final on Friday.
Algeria started brightly and came close on two occasions thanks to the dangerous Baghdad Bounedjah.
The striker first poked the ball past Kenneth Omeruo then shot straight at Daniel Akpeyi, before bursting through on goal only for Jamilu Collins to narrowly lift the ball over his own crossbar.
But Algeria took a deserved lead when Troost-Ekong turned Mahrez’s deflected cross into his own net from close range.
Nigeria equalised through Ighalo after Aissa Mandi was penalised, following a VAR review, for handling Peter Etebo’s shot.
That was just the second goal Algeria have conceded in the tournament, but they responded well, and Mahrez’s late strike kept alive their hopes of a second Afcon title.
Despite having one fewer day of rest than their opponents, Algeria looked well in control in the first half. Bounedjah could have netted at least once from the chances he had, but luck was not on his side. Nigeria, meanwhile, waited for opportunities on the counter-attack with the pace of Ahmed Musa and Samuel Chukwueze.
Troost-Ekong’s own goal came right in time for Algeria to gain more confidence, but Nigeria made use of their opponents running out of energy, having more of the ball and testing the Algeria goalkeeper until they equalised.
But, just as it appeared Algeria coach Djamel Belmadi was keeping all his substitutions for extra time, Mahrez was there to save the day in stoppage time with a brilliant free-kick.
This was a well-deserved win – they simply wanted it more than Nigeria.