The Segunda B club resisted admirably before Fran Garcia brought down 18-year-old Achraf Hakimi.
Marco Asensio coolly converted from the spot, but replays suggested the foul was actually just outside the box.
Paco Candela then hauled down Theo Hernandez at a corner, with Lucas Vazquez stroking in from 12 yards.
Candela was sent off for his foul on the Madrid left-back – it was his second yellow card in the seven minutes he spent on the pitch after coming on as a substitute.
And with two minutes to play, Madrid’s Spain Under-21 centre-back Jesus Vallejo was also sent off – a straight red card for a lunging challenge – on his debut for the club.
Madrid boss Zinedine Zidane made six changes from Sunday’s 3-0 win over Eibar, with key players such as Cristiano Ronaldo rested.
Barcelona have won the Spanish Cup in each of the past three seasons. They began their defence with a 3-0 first-leg victory at third-tier Real Murcia on Tuesday.
Zidane has never won the Copa del Rey as a player or a manager. As a player, he finished runner-up in 2002, when Madrid lost to Deportivo la Coruna, and in 2004, when they lost to Real Zaragoza.
The second legs are currently scheduled for 29 November.