Cesc Fabregas had already put Barca 2-0 up by the time Messi, who had been out since November with a torn hamstring, arrived from the bench shortly after the hour mark.
But the Argentina star added a late double of his own to ensure his side take an ominous lead into the second leg.
Messi almost set up Pedro minutes after joining the action but he soon found the back of the net himself, pouncing on a loose ball after Martin Montoya ran into trouble.
The returning star added his second three minutes later, in the third minute of stoppage time, when he raced onto Alex Song's through-ball.
Fabregas had opened the scoring with an eighth-minute header, before putting away the second from the spot in the 63rd minute after Valera brought down Pedro.
In the other games, Ruben Castro's 42nd-minute score was enough for Real Betis to beat Athletic Bilbao 1-0 while Dani Pacheco's 71st-minute score for Alcorcon saw the Segunda Division side account for Primera Division opponents Espanyol.
Racing Santander were held 1-1 at home by Almeria after Corona's first-half score was cancelled out by David Concha in the 65th minute, with Sundy Zongo Jonathan sent off for the visitors eight minutes from time.