Nigeria beat Algeria 2-1 in Dakar to win the Under 23 Africa Cup of Nations as South Africa took the remaining Olympic spot with a 3-1 penalties win over hosts Senegal in the third place play-off match.
Goalkeeper Jody February proved to be South Africa's hero with three saves during the shootout and a penalty save in the regulation 90 minutes which had ended 0-0.
Phakamani Mahlambi hit the winning spot-kick to send South Africa's men to the Rio Games.
It is the first time that both the South Africa men's and women's football teams have qualified for an Olympics.
In a rare instance of a third-place play-off match carrying more importance than the final itself, it was the hosts Senegal who had the best chance in the game to seal victory when they were awarded a penalty in the 73rd minute.
Moussa Wague's effort was saved by the outstanding February who then played a starring role in the ensuing shootout, saving three spot-kicks .
South Africa's men join the newly crowned Under 23 Africa Cup of Nations champions Nigeria and runners-up Algeria, at Rio 2016.
Nigeria took the lead in the final against Algeria thanks to a 14th minute penalty from captain Peter Etebo.
Just 16 minutes later, Algeria were level when Oduduwa Segun Tope scored an own-goal.
In a frenetic first half, Nigeria took what proved to be a decisive lead, as Etebo again struck - this time in open play - to put Nigeria 2-1 up.
Algeria missed a key opportunity to draw level in the second half.
USM Alger's Zinedine Ferhat had a penalty saved by Nigeria goalkeeper Daniel Emmanuel in the 68th minute.
Nigeria held on to win 2-1 to lift the trophy and take their place in Rio as African champions.
Nigeria boast a proud Olympics record. They made Olympic football history in 1996 by becoming the first African and non-European and South American team to win the gold medal.
They also finished runners-up at the 2008 Beijing Olympics, where current under-23 coach Siasia also coached the 'Dream Team' in China.
However, the so-called 'Dream Team V' led by Austin Eguavoen failed to qualify for the 2012 London Olympics after crashing out in the group stages of the 2011 U-23 championship in Morocco.