They will probably have to collect their luggage and then recheck it onto the domestic flight. However, the answer is it depends. Are they flying the same airline all the way through? Or are they flying one airline to Madrid and then switching to a domestic carrier once they get there?
Usually what happens is you come into Barajas, clear immigration, pick up your luggage, clear customs, recheck your luggage (usually right there if it's in the same terminal) and then go to your next gate. However things can get tricky if you are coming into Terminal 4, say and then have to get over to Terminal 1. Sometimes luggage does not have to be picked up for customs (but this is increasingly not the case) if there is nothing to declare and the passengers are on the same carrier, say Iberia from the US all the way to the final destination.
Depending on how many flights come in it can take as much as an hour or more to get through immigration (customs is generally quick) with 15-20 minutes to get luggage or it can take 15 minutes. There is no predicting it. We came from London last Thursday and it took us all of 15 minutes to get through immigration and there was no one even at customs. However when we came in last November it took about an hour.
If you know for sure the carrier your kids are flying comes into Terminal 4 then leave a good cushion of time for immigration, commuting between the terminals, delays, and baggage. Probably at least 3 hours would be good. They may have to wait longer than they like, but that is much much better than missing a flight.