Nigeria scraped through behind already-qualified Argentina, becoming the first African team to reach the last 16 at this tournament, while Switzerland also qualified along with France, who drew 0-0 with Ecuador. (Agencies)
Shaqiri and Messi will cross paths on Tuesday when Argentina meet Switzerland in their round of 16 match in Sao Paulo while France face Nigeria in Brasilia on Monday.
Iran's hopes ended in a 3-1 loss to World Cup debutants Bosnia, who enjoyed their first win at the finals while the Luis Suarez biting controversy rumbled on in the background.
Two days after Neymar scored twice in Brazil's 4-1 win over Cameroon on Monday, Messi replied with a brace of his own to leave the pair as joint top scorers at the tournament with four goals.
While Nigeria celebrated qualification, there was less joy for African elsewhere as Ghana became embroiled in a row over appearance fees on the eve of their decisive Group G match against Portugal which needed the intervention of President John Mahama.
In Porto Alegre, Messi lashed in a rebound after three minutes but Nigeria equalised a minute later with a superb finish by Ahmed Musa. But Messi was not to be outdone and curled in a brilliant 20-metre freekick in first-half stoppage time.
Musa levelled again with a clever finish two minutes after the restart but Argentina, who were assured of qualification before kickoff, got a fortunate third eight minutes later when a corner bounced in off the knee of Marcos Rojo.
Argentina finished top of Group F with a maximum nine points, followed by Nigeria (4), Bosnia (3) and Iran (1).
Bosnia, already eliminated after two losses in their first appearance at a major tournament since independence, went ahead in the 23rd minute through Edin Dzeko.
Miralem Pjanic doubled their lead before Reza Ghoochannejhad pulled a goal back for Iran in the 82nd minute and Avdija Vrsajevic restored Bosnia's two-goal cushion with a fine strike a minute later.
Switzerland defied 88 percent humidity in steamy Manaus as they went through from Group E.
Shaqiri fired Switzerland ahead in the sixth minute with a long-range effort similar to Messi's match-winning goal against Iran.
He struck again on the half hour with a clinical finish and rolled in Josip Drmic's pass in the second half to complete the tournament's second hat-trick following Thomas Mueller's effort against Portugal.
It was also the 50th hat-trick in World Cup history.
The Swiss (6 points) took second place in Group E behind France (7) who could not break down 10-man Ecuador, the only one of the sixth South American teams to fall at the first hurdle.
Ecuador had Antonio Valencia sent off for a challenge on Lucas Digne which left the France left back needing treatment in the second half.
Away from the action on the pitch, Luis Suarez's lawyer flew to Rio de Janeiro to present his defence after the Uruguay striker was accused of biting opponent Giorgio Chiellini during the 1-0 win over Italy on Tuesday.
"We're polishing off a defence argument," Alejandro Balbi told local radio in Uruguay, where many people support Suarez and are angry at what they feel is a "manhunt" being orchestrated by media in Europe.
FIFA said its disciplinary committee would work quickly to investigate the incident, with Uruguay due to play Colombia on Saturday in Rio de Janeiro in the last 16.
The group stage ends on Thursday when Germany and the United States, who have four points each, meet in Recife (1600) where both will qualify with a draw, while Portugal and Ghana, with one point each, clash in Brasilia in the other Group G game.
Algeria (3 points) need a win against Russia (1) in Curitiba (1700) to make sure of qualifying from Group H, although a draw should be enough. South Korea (1) face already-qualified Belgium in Sao Paulo and have only a remote chance of going through.
Ghana were expecting to receive their appearance fees after the Ghana FA promised to send the money by plane.
Nigeria scraped through behind already-qualified Argentina, becoming the first African team to reach the last 16 at this tournament, while Switzerland also qualified along with France, who drew 0-0 with Ecuador.