Cristian Tello put Barca ahead in the 14th minute but an own goal from Dani Alves and a second-half strike from Borges Romulo saw Spartak turn the game on its head and leave the Nou Camp shellshocked.
Messi, as he so often is, was Barca's hero though as he scored in the 71st and 80th minutes to leave Tito Vilanova's men top of Group G.
Barca coach Tito Vilanova said: "We carried the weight of the game and had the initiative throughout the whole match. We knew that their forwards were fast and that they were dangerous on the counter.
"I would have preferred to have won more easily and suffered less. However, I think these types of games are good for all of us. Football is not easy. The comeback proves that this team is eager to win.
"They proved that they really like playing football. It's difficult for a team that has won so much to be this hungry for victory."