Brazilian midfielder Ilsinho put Shakhtar in front at the stroke of half-time when he outmuscled Carles Puyol before lobbing Victor Valdes coolly.
The Ukrainians seemed to be in control at Olympiskyi, until Josep Guardiola finally decided to bring on Messi for Thierry Henry on the hour mark.
Shortly after his introduction, the Argentine ace fed Samuel Eto'o with a slick pass, but the Cameroonian stumbled to miss a golden chance for an equalizer.
Three minutes from time, Messi capitalized on a huge gaffe by Shakhtar keeper Andriy Pyatov, who spilled the ball as the Barca striker hit an empty net.
In the last minute of injury time, the livewire Messi secured the victory with a superb chip, following Xavi's deft through ball.
The Catalans lead Group C with six points from two games, while Shakhtar are level on three points with Sporting Lisbon, who beat Basel 2-0.