The do-or-die game was played at the Stade de Olympique in Nouakchott on Saturday evening.
The win saw the CECAFA region debutants progressed to the quarter finals of the Total Under-20 Africa Cup of Nations.
Both sides needed victory to assure passage to the last eight.
But it was Uganda who would pick the ultimate prize coming from behind to win the tie and seal second spot.
The host nation will now have to wait till the final round of Group C matches to know if they progress as one of the two best third placed teams.
Uganda fight back
The Mauritanians went ahead in the first half via a Silly Sanghare penalty.
However, Uganda equalized on the hour mark through Steven Sserwadda.
Kakooza would then step off the bench to calmly slot a late penalty for his second goal of the tournament.
Despite dominating for most spells of the opening half, Uganda went to the break trailing via a Sanghare penalty.
The Hippos did everything right in the opening half but score and were duly punished for their poor execution of chances.
Mauritania’s star man Oumar Mbareck was brought down inside the area by Abdu Azizi Kayondo with eight minutes left.
The fleet footed winger picked the ball inside the box before switching to his lethal left foot.
Kayondo duly tripped him as he prepared to pull the trigger.
Sanghare stepped up to the pressure plate and struck his spot kick into the roof of the net to give the home side a valuable lead.
A good move on the right saw Steven Sserwadda break into the box and hit a low shot that ended in the bottom right corner to equalise for Uganda.
The East Africans however kept the fight and won a penalty three minutes to time.
This was after the Mauritanian backline handled the ball inside the box as they tried to defend a corner.
Substitute Kakooza stepped up to convert for what would be the winning goal.