This is a standard answer " give me time "
There was a find in China some hatch-ling dinosaur in a nest , so the process was the same at that time, do you want more time ?
Yeah, if you like.
Eggs in general need to be kept at a fairly constant temperature in order to produce live young. That has been true going right back to before there was any life on the land on Earth.
In the ocean, keeping eggs at a constant temperature is usually not much of a problem. So, many fish, for example, just leave their eggs in safe place. On land, temperature variations are usually more extreme. Amphibians often carry their eggs around with them. Some reptiles and other creatures who lay eggs on land and then leave them often bury them in the ground in an attempt to reduce the temperature variation.
If you're going to lay eggs on or above the ground, then you start to need to actively control the temperature. Dinosaurs and their ancestors solved this problem by sitting on them.
Coming back to how such behaviours evolved, it is obvious. Those creatures, way back in the mists of the past, who did NOT keep their eggs at an appropriate temperature, died out because they did not produce live young. Those who happened to sit on their eggs for long enough managed to pass their genes on to the next generation.
Initially, the time required to sit on eggs would have been less than it is for the temperate-zone birds that exist today. If you live in the tropics, for example, temperature variations in the air are not as extreme.
All that is needed is
some small difference in survival rates between those animals who sat on their eggs for a longer time and those who sat for a shorter time. Given that, natural selection does the rest and tends to favour the longer sitters. That, in turn, allows creatures to move successful to climates where temperature variations are larger, and so on.
Complex behaviours don't appear overnight. They evolve step by step over many millions of years.