Just a second win of 2017 moves sixth-placed Villarreal to within two points of Sociedad and six of Atletico Madrid in fourth.
The visitors suffered the ignominy of a 4-0 thrashing at home to Roma in the Europa League on Thursday.
However, the best defence in La Liga recovered to record a 12th clean sheet in 23 games this season as Villarreal enjoyed the better chances.
Bruno Soriano headed against the bar from point-blank range and substitute Nicola Sansone had a strong penalty appeal waived away.
Yet, Villarreal got their reward with the last attack of the game as Jaume Costa's brilliant in-swinging cross was volleyed home first time by Castillejo.
Later on Sunday, Barcelona are also looking to bounceback from a 4-0 mauling at the hands of Paris Saint-Germain in midweek when they host lowly Leganes at the Camp Nou.