Captain Thembi Kgatlana scored the winner in the 92nd minute to make history for South Africa and set up a meeting with the Netherlands — sending Italy home.
Arianna Caruso scored twice for Italy and thought she had salvaged the draw they needed to go through with 16 minutes left, before Kgatlana's stunning late intervention.
The Italians were in tears at the end as their opponents celebrated a famous win.
"Over the last two weeks, I've lost three family members. I could have gone home but I chose to stay with my girls," said Kgatlana, who plays for Racing Louisville in the United States.
"Because that's how much it means."
Coach Desiree Ellis, whose side were beaten 2-1 in the last minute in their opener against Sweden and then drew 2-2 with Argentina, added: "This is just amazing. I thought they were incredible."
"I said we were going to fight for every ball. We said we're not going to give up, and they were absolutely magnificent. This victory is for everyone back home, people that got up in the early hours of the morning to support us — that is for them."
Sweden topped Group G with nine points, followed by South Africa with four and Italy with three. Argentina finished bottom on one.
Caruso scored an 11th-minute penalty to put Italy on the way but they conceded an equalizer in on 32 minutes.
Italy defender Benedetta Orsi received the ball and, without looking up to see where Francesca Durante was, pinged the ball back to her goalkeeper.
But Durante was nowhere near where the backpass went, and the ball ended up in the Italian net for a glaring own goal.
Italy hit the post in a goalmouth scramble just moments later, but there was little to choose between them at the break in a wet Wellington.
The African champions knew they needed to score and they went on the attack in the second half.
South Africa duly took the lead on 67 minutes when skipper Kgatlana played in Hildah Magaia, and she swept the ball into the net.
South Africa very nearly got a third, but Durante made a stunning save and minutes later came what looked like the defining moment of the match.
Italy drew level through Caruso for her second and, after an agonizingly long VAR review, the goal was given for 2-2.
But the Italian defense went missing again in the second minute of injury time at the end of the game, and Kgatlana slotted in to send South Africa through.
"They fought like warriors. They fought like the heroines that we know that they are," said Ellis. They've made history — not just getting our first win but a place in the round of 16. And that is freaking amazing."