Mirandes, currently in mid-table, claimed a third successive victory over a La Liga side in this year's tournament after earlier knocking out Celta Vigo and Sevilla.
They previously reached the last four in 2012 when they were still a third division outfit, eventually being defeated by Athletic Bilbao.
Brazilian striker Matheus Barrozo gave the home side the lead after 17 minutes before Javier Ontiveros levelled just after the half-hour mark for Villarreal who currently sit in seventh place in the top flight.
Martin Merquelanz restored Mirandes' advantage with a penalty on the stroke of half-time.
Santi Cazorla equalised 10 minutes into the second period with another penalty.
However, two minutes later, Mirandes were back in front thanks to Odei Onaindia with Antonio Sanchez making it 4-2 in stoppage time.