AFCON 2015: Senegal 0 Algeria 2
Senegal headed into the final round of games as group leaders, but their loss in Malabo coupled with Ghana's comeback win over South Africa in Group C dumped Alain Giresse's team out of the tournament.
Leicester City winger Mahrez capitalised in the 11th minute as a Senegal line-up, sent out in a reshaped 3-5-2 formation, struggled badly in the face of Algeria's vibrant attacking play.
Giresse's men muscled their way back into the contest and controlled much of the second half before Bentaleb beautifully converted Sofiane Feghouli's pass from long range with eight minutes remaining to see his nation through as group runners-up.
Algeria passed up a glorious chance to lead the winner-takes-all clash inside two minutes after a dreadful error from Senegal defender Papy Djilobodji left Feghouli through on goal, but a heavy touch allowed goalkeeper Bouna Coundoul to collect at his feet.
The Valencia player did not rue his miss for long as Mahrez exploited a yawning gap between right wing-back and centre-back to control Madjid Bougherra's free-kick from deep and finish calmly.
Coundoul held an audacious 35-yard free-kick attempt from Faouzi Ghoulam and the keeper spared his ailing team-mates in the 21st minute when he pushed away a rasping strike from El Arbi Hillel Soudani.
Sadio Mane looked to haul Senegal back into the contest with a driving run, but the Southampton forward sliced wildly over having broken into the Algeria box.
Senegal were forced into a 29th-minute change, Pape Souare replacing Cheikh M'Bengue after the latter suffered a head injury in a challenge with Aissa Mandi, but Algeria had let their early intensity drop as Djilobodji flashed a free-kick wide.
Goalkeeper Rais M'Bolhi was out sharply to deny Mame Biram Diouf and, after Nabil Bentaleb required a last-ditch block to thwart Stephane Badji in stoppage time, Algeria were grateful for the half-time whistle.
Senegal introduced Papiss Cisse in place of midfielder Pape Diop for his first action of the tournament nine minutes into the second half and Carl Medjani was forced to clear behind as a dangerous Diouf cross searched out the Newcastle United striker.
Mane's footing deserted him when Badji drilled a low ball into the box after 61 minutes, a deflection off the referee falling in Senegal's favour as they opened up Algeria down the right flank.
For all the pressure applied by Giresse's men, the best opening of the second period fell to Algeria - Kara Mbodj clearing off the line from Saphir Taider after Coundoul spilled Mahrez’s 75th-minute cross
Dame N'Doye, on for Mane, slashed a shot over before Bentaleb showed him how it was done and Ghana sealed Senegal's fate.
Watch every match of the Africa Cup of Nations LIVE on British Eurosport HD or sign up to Eurosport Player and follow the action on mobile, tablet and other devices: www.eurosportplayer.com