Barcelona needed an injury-time winner from Clement Lenglet to edge past third division side Cultural Leonesa in the first leg of their Copa del Rey tie.
The last-32 game looked set to be a goalless draw, but French defender Lenglet headed home his first Barca goal from Ousmane Dembele's free-kick.
Barca, winners of the cup for the last four seasons, saw midfielder Sergi Samper limp off in the first half.
The second leg takes place at the Nou Camp on 5 December.