Barcelona needed a second-half comeback to beat Rayo Vallecano 2-1 in the Copa del Rey round of 16 and advance to the quarterfinals on Wednesday night at the Estadio de Vallecas.
Despite their talisman's return, Barcelona could not find a way through in an opening 45 minutes they dominated, twice hitting the post and forcing Rayo keeper Stole Dimitrievski into a good save from an Antoine Griezmann strike as the half wore on.
Barcelona's misfortune continued in the second half when Messi's audacious attempt at goal from a free kick hit the far post as it looked to be curling into the Rayo net.
After that, Rayo grew into the game and took a surprising lead shortly after the hour with a tap-in from Fran Garcia when Barca keeper Neto spilled a save right into his path in front of goal.
The lead was short-lived, though, as Barca drew level through Messi on a counter-attack when the Argentine was left with the easiest of back-post finishes thanks to a nice cross from Griezmann.
Soon after, Barcelona took the lead as Jordi Alba set up Frenkie de Jong for another close-range finish that put the visitors in position to see out the tie and book passage to the quarterfinals of Spain's annual cup competition.
Messi should have added a third goal in added time after waltzing his way through the Rayo defence, but when it came time to shoot he lost his footing and could only find the side netting.
"The truth is that was a difficult game, we really should have put it to bed far earlier, but the most important thing is we've got through," said Barca defender Ronald Araujo.
"Their strategy was to play with a very high defensive line but luckily we were able to play passes into space and find both goals that way."
Barca coach Ronald Koeman talked before the game of his desire to win the Copa, with his team looking unlikely to catch runaway La Liga leaders Atletico Madrid, and he backed up his words by fielding a strong side.
The Dutchman took the surprise decision to start Messi and also fielded Sergio Busquets, Griezmann and De Jong, who is in scorching form in an advanced midfield role.
But Barca, who have lifted the Copa del Rey a record 30 times, were still lacking something until Koeman made a triple change just after going behind, introducing Alba, Ousmane Dembele and teenager Pedri.
"We were excellent from the start and let them off the hook on various occasions, messing up the final pass," Koeman said.
"We then showed our character when we went behind and produced a lot of quality to score the two goals."
Sevilla joined Barca in Friday's quarterfinal draw by crushing 2019 Copa winners Valencia 3-0 at home.