Argentina were 1-0 up at half-time thanks to a Nicolas Otamendi goal after just six minutes.
Panama lost Anibal Godoy to a red card after half an hour but the Central Americans held on until Messi, who missed Argentina’s opening win against Chile with a sore back, replaced Augusto Fernandez after 61 minutes.
The Barcelona forward started gingerly but grabbed Argentina’s second in the 68th minute by steering home a left foot shot after a rebound fell to him in the Panama box.
He scored a gorgeous second with a 25-yard free kick just nine minutes later and then with some fancy footwork in the box gave himself enough space to slot home his third and Argentina’s fourth in the 86th minute.
Sergio Aguero, another substitute, made it 5-0 with a header in the final moments.
“My back was a bit painful at the start but it went away,” said Messi, who was cheered every time he touched the ball by the 53,000 crowd in Chicago.
“I think we are where we want to be, which is getting better,” he told TV cameras after the game.
The result came after Chile had beaten Bolivia in the other Group D game. Argentina have six points from two games, while Bolivia have none and are out.
Chile and Panama both have three and will face off on Tuesday to decide who join Argentina in the quarter-finals.
Goals in the first and last minutes of the second half from Bayern Munich midfielder Arturo Vidal were enough to help the reigning champions overcome Bolivia in Boston.
Chile had 75 per cent of the possession and 18 shots at goal to Bolivia’s three and were far superior against the lowest-ranked team in the tournament.
Vidal put the Chileans ahead in the 46th minute but substitute Jhasmani Campos got an equalizer for Bolivia on the hour mark with a superb free kick.
Vidal then took advantage of a controversial penalty decision for handball to score the winner in the dying seconds.