Algeria crashed out of the 2023 Africa Cup of Nations as minnows Mauritania claimed a famous first ever win at the finals to snatch a spot in the last 16.
Centre-back Yali Dellahi swept home the winning goal late in the first half to stun the two-time Afcon winners.
Defeat means 2019 champions Algeria exit the tournament at the group stage for the second successive edition.
They finish bottom of Group D while Mauritania will definitely progress as one of the best third-placed teams.
Mauritania will face surprise Group B winners Cape Verde in the last 16 at Felix Houphouet Boigny Stadium in Abidjan on Monday (17:00 GMT).
Algeria join fellow former champions Ghana in being eliminated after results earlier on Tuesday meant the Black Stars, with just two points to their name, cannot qualify as one of the top four third-placed sides.
The Lions of Chinguetti had to withstand late pressure and survive more than 11 minutes of stoppage time as Algeria sought a leveller that would have sent them through at Mauritania's expense.
But the underdogs were good value for a victory that sent them into the knock-out stages alongside second-placed Burkina Faso and Angola, who sealed top spot in the group by beating the Burkinabe 2-0.
Abject Algeria punished by magnificent Mauritania
Algeria had drawn their opening two group games in Ivory Coast and knew another draw at the Stade de la Paix would secure qualification as a point would put them among the four best-ranked third-placed teams.
Nothing but a victory would do for their opponents, however, after they had lost their first two matches - and that contrast was evident throughout with Mauritania far more purposeful and threatening in their play.
They had to negotiate a slightly nervy opening half-hour, when Houssem Aouar volleyed wide a decent chance for Algeria, but were more than a match for the North Africans from then on.
Mauritania's first chance saw Souleymane Anne thwarted by Anthony Mandrea but Algeria failed to clear the subsequent corner and Dellahi capitalised to send the underdogs into raptures.
They should have made their margin of victory more comfortable, wasting a host of second-half chances against an Algeria side repeatedly run ragged on the counter.
But Aboubakary Koita blazed over after a brilliant solo run, Mandrea saved well from Aboubakar Kamara and Pape Ibnou Ba hit the bar from close range when it seemed easier to score than miss.
Algeria, though, lacked the nous to punish Mauritania for those misses, although the half-time introduction of Riyad Mahrez, surprisingly dropped by boss Djamel Belmadi, did provide more creativity.
The ex-Manchester City winger was the architect of their best chances, with his crosses setting up Hicham Boudaoui's header and Aissa Mandi's volley, which were both brilliantly saved by Babacar Niasse.
The Mauritania goalkeeper made one more key save to foil Mandi amid a tense near-dozen minutes of time added on before the final whistle yielded scenes of jubilation for the north-west Africans, who had lost six of their previous eight matches at Afcon finals, drawing the other two.
Their victory marked another major triumph for coach Amir Amdou, who was in charge of Comoros as the islanders reached the last 16 on their debut at the 2021 finals before losing to hosts Cameroon.
Algeria had the best record of any team in qualifying for the 2023 finals but several of their players left the field in tears and they bow out having not won a single game in the tournament's two editions since lifting the title four years ago.