The game had looked to be heading to extra time as the two teams battled for the right to play Senegal in Friday's showpiece, but Manchester City midfielder Mahrez won it for Algeria late on.
Algeria went ahead courtesy of a first-half own goal by William Troost-Ekong, with the Udinese central defender turning a Mahrez cross into his own net.
Odion Ighalo equalised with a penalty for Nigeria, with the spot-kick awarded for handball after a VAR review.
An additional 30 minutes seemed likely but Mahrez settled the tie with what proved to be the last kick of the match, curling in a free-kick from just outside the area.
Algeria will play Senegal in the final after their 1-0 extra-time win against Tunisia earlier on Sunday.