The twice World Cup champions will face France in the last 16 while Croatia, who advance as winners of Group D with the maximum nine points after beating Iceland 2-1, will take on Denmark.
Nigeria were just minutes away from progressing before central defender Rojo superbly volleyed home a Gabriel Mercado cross from the right.
As the drama unfolded in St Petersburg, Iceland were simultaneously fighting with all their Nordic might for a win over the Croats in Rostov-on-Don that could have sent them through.
The Argentines, however, had enough on their plate with a feisty Nigeria to worry about results elsewhere.
Messi had put them ahead in the 14th minute, with a fabulously taken goal but the Africans equalised through a Victor Moses penalty in the 51st minute.
The South Americans struggled to respond to that setback with a ragged second half display.
For all their attacking talent, it was their centre half Rojo who delivered the decisive blow with a powerful and accurate strike that Messi himself would have been proud of.
The game began in ideal fashion for Jorge Sampaoli’s side, who had managed just a point from their opening two games.
A perfectly weighted diagonal ball over the top by Ever Banega was sublimely brought down on the outside of his thigh by Messi who then drove past Nigeria’s teenage keeper Francis Uzoho to open the scoring.
The selection of Banega proved to be an inspired one by Sampaoli, with the Sevilla midfielder providing the quality service that had been lacking so far in Russia.
It had been an impressive half from Argentina but the game was to take a twist shortly after the interval.
Nigeria wasted several chances to add a second and secure their place in the last 16 and they were furious that a video assistant referee (VAR) review failed to award them a penalty for a possible handball by Rojo.
At the other end, Gonzalo Higuain blasted over when free in the area, a miss that could have haunted him almost as much as those from the loss to Germany in the final four years ago in Brazil.
But saving Higuain and his teammates was the unlikely source of Manchester United central defender Rojo who kept Argentina’s tournament alive and left Nigeria, who had been positive and aggressive throughout, in total despair.
“It is what it is. It just wasn’t to be,” said captain John Obi Mikel.
“It is a young team, though, and in four years most will be ready for this tournament. I’m very proud of the boys, they’ve done really well.”
While Nigeria will look to the future, Argentina are very much in the present as they head into the knockout stages for the fourth consecutive World Cup finals, since being eliminated in the group stage in 2002.