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.