African Cup of Nations debutants Cape Verde came from behind to score an injury-time winner and beat Angola 2-1 to reach the quarter-finals.
Hosts South Africa trailed twice but earned a 2-2 draw with Morocco and also go into the last eight after finishing top of Group A. South Africa's result meant that Cape Verde qualified in second place with Morocco finishing third and missing out.
Cape Verde captain Fernando Neves's own-goal in the 33rd minute had his team trailing and on the verge of being eliminated. But Fernando Varela headed in after a goalmouth scramble in the 82nd minute to level, and Ramos's late winner saw Cape Verde's entire squad run on the pitch to celebrate.
Ramos powered his shot into the roof of the net after Angola goalkeeper Lama parried a cross into his path to set off the chaotic celebrations, led by Cape Verde coach Lucio Antunes sprinting up the touchline.
Antunes had insisted that his team from the tiny island nation was good enough to be taken seriously. Cape Verde's entire team then burst into the post-match news conference to chant and sing and dance in front of reporters, with Antunes draped in the blue flag of his country. "We came here with the determination to win this game and from the first minute we worked," said Antunes, who then sang what he called a traditional Cape Verde song.
Goals from Issam El Adoua and substitute Abdelilah Hafidi appeared to have put Morocco through for the first time since 2004. But both times South Africa fought back, with May Mahlangu and Siyabonga Sangweni ensuring the hosts topped their group on goal difference. Defender Sangweni, who scored in South Africa's 2-0 win over Angola, again made a crucial contribution as his 87th-minute goal kept Bafana Bafana in the tournament.
"If you know there are only a few minutes left I think you throw everything you have forward. He's gone up and taken a chance and it's paid off," South Africa captain Bongani Khumalo said. "It gets tougher now."
Morocco took the lead after 10 minutes when El Adoua beat Khune to a corner to nod home. But Mahlangu found space to curl his shot into the top right-hand corner to make it 1-1.
With Angola leading 1-0 at the time, Morocco needed another goal to progress and they found it when the ball dropped to Hafidi, who fired home through several players.
But South Africa equalised again when Sangweni mirrored Mahlangu's goal from the edge of the area. With news of Cape Verde's win coming through, Morocco threw everything forward, but South Africa held on.