In the United States, for example, there's no single federal law mandating schooling. Instead, individual states enacted compulsory attendance laws, starting in Massachusetts in 1852. These laws built upon earlier efforts to establish public schools, often driven by a combination of social, economic, and religious factors.
Therefore, there's no single answer to your question. The "need" for schools arose from various societal pressures, and the legal frameworks enforcing attendance evolved over time in different jurisdictions.