Nigeria will also join Alejandro Sabella's side in the last 16 as Bosnia's 3-1 win over Iran in Salvador meant that four points would be enough for them to finish the group in second place.
It took Argentina just three minutes to take the lead after Angel Di Maria’s strike rebounded into the path of Messi and he slammed the loose ball into the roof of the net.
But Nigeria hit back within a minute as Ahmed Musa cut inside of Pablo Zabaleta before unleashing a wonderful curling effort beyond Sergio Romero from the left wing.
Messi looked hungry to make it four goals from three games in Brazil and he did so just before the break, finding the net with a stunning free-kick which left Nigeria keeper Vincent Enyeama helpless as he scrambled across his line.
It promised to be the Messi show but Nigeria had other ideas as they hit back just minutes after half-time when Musa was slotted through on goal and he kept his composure to fire beyond Romero for his second of the evening.
Messi then turned provider when his corner was flicked into the path of Marco Rojo to put Argentina back in front from inside the six-yard box, before the talismanic captain left the field to a standing ovation after an hour.
Argentina will now face the runners-up of Group E Switzerland while Nigeria must play winners France.
Messi's double means he is now joint top-scorer with Neymar on four goals and while the number 10 was substituted and spared the final half an hour of the game, his emphatic form means Argentina will go into the competition's latter stages in high spirits.
They got the game off to a flying start as Javier Mascherano fired an incisive pass to Angel Di Maria and while the winger's shot was initially saved by Enyeama, the ball cannoned back off the near post and into the path of Messi - who shot high into the net.
One minute and 20 seconds later, however, Nigeria were level. Michel Babatunde broke from midfield and played the ball left to Musa who skipped away frpom Zabaleta before curling the ball brilliantly past Romero and into the far corner.
Argentina almost restored their advantage soon after when Messi slid in Gonzalo Higuain but the Napoli striker could only find the side-netting from an acute angle after dribbling round the goalkeeper.
After a pulsating opening 10 minutes, the game tightened up with Nigeria successfully able to stifle Argentina's attacking talents.
Di Maria skewed a shot wide from the edge of the area just before the half-hour mark and Peter Odemwingie fired a long-range shot over the bar as both sides struggled to create clear-cut chances.
Sergio Aguero's injury-plagued season continued as the Manchester City forward hobbled off in the 37th minute and was replaced by Ezequiel Lavezzi.
Two minutes before the break, Messi fired a bending free-kick from 40 yards towards the top corner but Vincent Enyeama was able to dive across and push the ball past the post.
Moments later the Argentina number 10 was given a second opportunity, slightly closer in, and this time he made no mistake, chipping the ball over the wall and beyond Enyeama to make it 2-1 on the stroke of half-time.
The second period began just as frantically as the first - with two more goals in the opening five minutes.
Nigeria drew level for the second time in the match and again it was Musa with the strike, as he ghosted through the Argentina defence before slotting past Romero to make it 2-2.
The Super Eagles' parity was short-lived, however, as the lively Di Maria unleashed a fizzing shot from the edge of the area which Enyeama did well to palm behind.
From Lavezzi's resulting corner Argentina found the net as Nigeria's defence failed to clear and Rojo was left free to turn the ball home from close range.
Messi was denied the chance to complete his hat-trick with Sabella opting to rest his talisman and bring on Ricardo Alvarez shortly after the hour.
The South Americans almost extended their lead in the 78th minute when Di Maria's dinked free-kick allowed Lavezzi to volley from close range but his shot was blocked by the feet of Enyeama.
Nigeria then went close at the other end as Musa again cut inside Zabaleta on to his right foot but this time the Argentina full-back read the move and was able to block the shot.
Ezequiel Garay should have put the result beyond doubt with a header at the back post in the final 10 minutes but it proved inconsequential as both teams booked their spots in the knockout stages.