Militao, having had two headers saved by Sergio Herrera in the first-half, got the better of the goalkeeper with another in the 76th minute and Casemiro's finish doubled the lead four minutes later.
The victory for Zinedine Zidane's second-placed side came after Atletico had gone five points clear earlier on Saturday with a 1-0 win at Elche.
Third-placed Barcelona, three points behind Real, are at Valencia on Sunday, and Sevilla, a further point back in fourth, host Athletic Bilbao on Monday.
Four changes to Real's side from the 1-1 Champions League draw with Chelsea included a first start since January for Eden Hazard, who struck off-target in the third minute.
Thibaut Courtois saved a Chimy Avila effort at the other end, before Casemiro unsuccessfully tried his luck with a shot from his own half just before the quarter-hour mark.
After Courtois dealt with a delivery from Manu Sanchez, Real were then denied by two fine saves in quick succession from Herrera as he kept out a Hazard shot and Militao header.
And soon after there was further good work from Herrera as he saved another headed attempt by Militao.
Vinicius Jr subsequently fired into Herrera's side-netting, and Avila had the ball in the Real net just before the interval but the effort was ruled out for offside.
After the break, Real's attempts to break the deadlock saw Militao and Rodrygo put shots wide before the former finally beat Herrera as he connected with Isco's corner. Casemiro then added the second, sending the ball past Herrera having been fed by Karim Benzema.