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 leveled 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 equalized through Ighalo after Aissa Mandi was penalized, 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.
Algeria will now face Senegal in the final on Friday.