Villarreal were the better side in the first-half but failed to capitalise until Manu Trigueros fired them in front on 50 minutes, with Cédric Bakambu doubling the lead six minutes later.
Substitute Álvaro Morata headed the decisive goal on 83 minutes to put Madrid back on top, a point ahead of arch-rivals Barcelona with an extra game to play.
Madrid welcome Bale back to starting XI
Villarreal made a while host of changes to the team that beat AS Roma in the Europa League but crashed out 4-1 on aggregate on Thursday, welcoming back regular starters such as Mateo Musacchio, Mario Gaspar, Trigueros and Bakambu.
Former Tottenham Hotspur striker Roberto Soldado, who started against Roma, started on the bench with the hope of making his first league appearance of the season after battling back from a cruciate ligament injury.
Madrid played the same starting line-up that lifted them to their 11th Champions League success in Milan 271 days ago, with winger Gareth Bale making a first start of 2017 after back-to-back cameos off of the bench.
His return meant that Madrid's front three of Bale, Karim Benzema and Cristiano Ronaldo featured together for the first time since October, with Pepe also returning to line-up in place of the injured Raphaël Varane.
But the forward triumvirate known as 'BBC' took time to grow into the game, the Yellow Submarine showing exactly why they boasted the best defence in Spain coming into this affair with just 15 goals conceded in 23 games.
The pressure was on the visitors after Barcelona had moved back ahead of them with a late win over Atletico Madrid earlier in the day, but an understated start saw both sides wrestle for control in the opening 20 minutes.
Ronaldo dragged a harmless shot wide of goal after cutting in from the left, while Keylor Navas had kept out Mario Gaspar's stabbed attempt which came after Trigueros' run and lay-off.
Villarreal fail to take chances while on top
Los Blancos' first real opportunity didn't come until the 24th minute, Marcelo's left-sided cross following good footwork finding Benzema a few yards out at the back post, but Sergio Asenjo reacted well with his left hand to palm the Frenchman's header clear.
Villarreal were increasingly dangerous on the counter-attack, the lively Samu Castillejo's sprint down the right finding Trigueros. With his back to goal, the midfielder teed up Jonathan dos Santos on the edge of the box but his drilled strike was deflected wide.
Fran Escribá's charges should have taken the lead on the half-hour mark. Bakambu's failure to get a good headed connection on Jaume Costa's cross allowed the ball to fall for the unmarked Castillejo behind him, but he could only fire over Navas' crossbar in stretching for a half-volley.
Goalkeeper Asenjo - having endured a torrid injury record - was forced off because of an innocuous issue sustained in keeping Benzema's header out after 35 minutes with Andrés Fernández handed his first La Liga appearance for Villarreal in his place.
Yet the replacement goalkeeper, on loan from Porto, wasn't called into any meaningful action until the second-half with Zinedine Zidane's side lacklustre, having also struggled in defeat at Valencia on Tuesday.
Real Madrid had not failed to score in 43 games across all competitions, when they drew 0-0 with Manchester City in the Champions League semi-final first-leg last April.
And they forced Fernández into a first save when Bale cut into the box from the left and fed Benzema to curl a first-time right-footer towards goal, but it was far too central to trouble the Villarreal shot-stopper.
Home side fire twice in quick succession
Trigueros finally put the home side ahead on 50 minutes, Marcelo's clearance from Adrián Lopez's cross falling for Trigueros to power in a bouncing ball low beyond Navas and into the bottom left corner for his sixth of the season.
And within minutes it was 2-0 for Villarreal as Real Madrid were made to pay for their lethargy, captain Bruno Soriano allowed to stroll through the centre and find Bakambu with a well-weighted pass. The Congolese striker's first touch took him away from Sergio Ramos before he slid past Navas and inside the near post.
That meant Madrid had slipped to a two-goal deficit for the second successive match, having lost their record of remaining unbeaten whenever they had conceded first at Valencia, and left them needing three goals in order to return to the top of the league table.
Zidane sacrificed Casemiro for Isco in the wake of that second goal as they looked for a response, while Adrián was replaced by former Madrid winger Denis Cheryshev.
Madrid complete three-goal comeback to steal victory
Just moments later, Real Madrid talisman Ronaldo was denied by the woodwork. He executed a left-footed volley to perfection after a deflection carried the ball his way, only to watch his fierce attempt cannon back off the far post before Benzema's follow-up was deflected over.
Yet Zidane's charges gained a precious foothold back into the game just four minutes later when Carvajal stood up an excellent cross at the back post for Bale to bury with a header on his first start in four months, making it 2-1.
Madrid continued to commit bodies forward, carrying significantly more threat than they had before getting back on the score-sheet through Bale - though Ronaldo swiped a volley over from the edge of the area.
Yet the Portuguese wideman was handed the perfect opportunity to level when Toni Kroos' shot struck Bruno Soriano's arm and referee Jesús Gil Manzano harshly pointed to the spot.
The Real Madrid No.7 confidently dispatched the resultant penalty, firing beyond the 'keeper into the bottom left, for his 16th goal of the season to bring Madrid level.
That paved the way for a bombardment in the final 15 minutes, Real Madrid eventually completing their turnaround after 83 minutes through second-half substitute Morata.
Following a move in which Villarreal were crying for a from Isco's challenge, Marcelo's sublime delivery from the left allowed Morata to header low past Fernández.
Ramos headed over late on, while Villarreal scrambled for another equaliser, but Real Madrid held on to a huge three points which returns them to the summit and maintains at least some grip on the league title.