Mauritania had never led in any of their previous eight matches at the continental finals, let alone won a game, but a first half goal from Yali Dellahi was enough to send Algeria packing in a second successive embarrassing early exit for Djamel Belmadi's side, after a similar failure in 2021.
Angola took top spot in Group D with seven points from three games, ahead of Burkina Faso who they beat 2-0. Mauritania will be among the four best third-placed sides and earn a knockout spot, facing Cape Verde next, as Algeria finished bottom of the group.
Algeria look a shadow of the side that lifted the trophy in Egypt in 2019, and have not won an AFCON match since then. They dropped captain Riyad Mahrez to the bench against Mauritania but brought him on at halftime in a desperate bid to reverse their fortunes which proved fruitless in the end.
Mauritania took the lead in the 37th minute after a poor clearance from Algeria defender Mohamed Amine Tougai fell to Dellahi in the box and his shot beat goalkeeper Anthony Mandrea and crept in off the post.
It should have been 2-0 when Aboubakary Koita went on a mazy run in the second half and worked space for a shot but his effort was wild and spoiled what would have been one of the goals of the tournament.
Mauritania keeper Babacar Niasse then produced a superb save to keep out Hicham Boudaoui's bullet header before he managed an even better reflex stop to deny Aissa Mandi from close range.
As Algeria pushed forward in search of an equaliser, they inevitably left gaps at the back and substitute Pape Ibnou Ba should have added a second for Mauritania late on but somehow missed the target from close range.
They were instead forced to survive 11 tense minutes of added time, doing so to secure a victory that will live long in the memory.
Additional reporting by Reuters.