Zinedine Zidane's side had won just once in four outings going into the last-32 tie, including falling to a disappointing Supercopa de Espana semi-final exit to eventual winners Athletic Bilbao.
However, while that result was a setback, Madrid can expect to face a much more severe backlash after being on the wrong end of a famous upset at the tiny Estadio El Collao.
Eder Militao rewarded the visitors' dominance when heading his team in front on the stroke of half-time, yet Madrid failed to build on the centre-back's first goal for the club.
The host defended in stoic fashion to keep the gap down to one before grabbing an unlikely equaliser with 10 minutes remaining, Jose Solbes poking home from close range at a corner.
Alcoyano had Ramon Lopez sent off in the second period of extra time but the 10 men grabbed an unlikely winner through Juanan, who volleyed in Ali Diakite's cross in the 115th minute to seal a famous result.