Players react after the final whistle of the Qatar 2022 World Cup Group A football match between Qatar and Senegal at the Al-Thumama Stadium in Doha on November 25, 2022. AFP
Goals either side of half-time from Boulaye Dia and Famara Diedhiou gave Senegal the lead before Qatar substitute Mohammed Muntari replied in the 78th minute to set up a dramatic finale.
Senegal substitute Bamba Dieng scored six minutes later to put the result beyond doubt and lift the African champions, who opened with a 2-0 loss to the Netherlands, right back into contention in Group A.
Defeat left bottom side Qatar sweating on results elsewhere if they are to avoid following South Africa in 2010 as only the second World Cup hosts to bow out after the first round.
Their elimination would be confirmed if Ecuador fail to beat the Netherlands later in the day, and would make Qatar the first World Cup hosts to go out after two games.
"Today we showed what we are able to do. We were competitive and we played well," said Qatar manager Felix Sanchez.
"Obviously the result is key but we showed a better performance. We have to get ready to play against the Netherlands.
"We know that it's going to be an even tougher game against them."
Qatar had made a nervy start to the tournament in a 2-0 defeat to Ecuador and they again looked tentative against Senegal.
The Asian champions dropped their goalkeeper Saad Al Sheeb to the bench after his shaky performance in the opener.
His replacement Meshaal Barsham did not inspire much confidence in the early stages, flapping at a corner as Senegal pushed for the opening goal.
Ismaila Sarr and Nampalys Mendy had already missed scoring chances for Senegal by then, and Idrissa Gana Gueye and Youssouf Sabaly squandered more as the first half wore on.
Qatar almost out
Qatar spent most of the opening 45 minutes on the back foot but a rare break forward saw Akram Afif incensed not to be awarded a penalty after he was bundled over by Sarr.
Senegal looked like they would head into half-time without managing to break down Qatar's resistance, only for the hosts to gift them a goal in the 41st minute.
Qatari defender Boualem Koukhi fluffed a clearance to leave Dia to pounce on the loose ball and fire it past Barsham.
Senegal doubled their lead almost immediately after the break when Diedhiou glanced home a header at the near post from a corner.
Qatar began to show much more attacking ambition from two goals down and Afif was involved in everything.
Almoez Ali forced a fingertip save out of Senegal goalkeeper Edouard Mendy before the Chelsea man again rescued the Africans from Ismail Mohamad's effort.
Mendy was rooted to his line when Muntari pulled one back for Qatar, powerfully heading home Mohamad's cross to haul the hosts back into it.
The goal lifted the crowd and sent Qatar surging forward in search of another, but Dieng nipped the revival in the bud in the 84th minute when he swept the ball home for Senegal's third.
Abdelkarim Hassan then lashed a free-kick just wide of the post as Qatar tried to hit back but Senegal held firm to see out the win.
"We had a few chances and we managed to score a goal but then at the end we didn't have as many chances as we wanted," said Sanchez.
Check the 2022 World Cup schedule and groups standings.
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO_Sports and on Facebook at AhramOnlineSports.)