Just wondered how it came about, sorry if this is the wrong sub to ask this question
This is the right sub! Virtually all countries with formal education have a three-level system:
This particular model can be traced back to Greek and Roman structures of education that moved young men through different settings as they aged. A German educational reformer in the 1630's, Johan Amos Comenius, was inspired by writing about the grammaticus and advocated for a similar system. This eventually evolved into the Prussian model, which informed the UK and America. The evidence of his reform advocacy can be seen today in the name of the Boston Latin School, America's first (1635) high school. Comenius' use of Latin as a synonym for "secondary school" referred to the common curriculum, known as a "classical" or "Latin", for older students of his era. It was this naming convention Boston fathers with means used when creating a place for their sons to prepare for Harvard (which, incidentally, considered pretty much any white boy/man who wanted to attend, regardless of age.)
While different schools and counties in England have some minor policy differences, the UK has a federal education system and the country has generally settled in a primary and secondary system that serves all young people, ages 5ish til 19ish. Through a combination of factors, including tradition, population density, policies, and laws, UK schools are mostly either grammar or high school.
American schools likely would have followed a similar pattern, as early education advocates were informed by European systems. However, there are a few things that gave rise to a different path on this side of the pond. As the 10th Amendment of the US Constitution does not mention education, it's up to each state to determine how children living within its borders receive an education. Compulsory education for all children, including those with disabilities, was the law in the UK by 1893 with the passage of The Elementary Education (Blind and Deaf Children) Act. Although most American states had compulsory education laws by 1920 or so, in many cases, they were dead letter laws or in the case of Mississippi, were rescinded in the 1950's, just before the decision that would lead to the desegregation of schools, so that schools could legally turn away Black children. It wouldn't be until the passage of the Education for All Handicapped Children Act in 1975 that American school districts had to accept every child within their attendance boundaries, regardless of race, disability or immigration status.
All of that to say, while there are some national policies, there is a high degree of variation across the states and each state ed system has its own history and culture. Not all American states have an "extra" school in the progression from kindergarten to Senior year. Some states have just grammar (or elementary) and secondary (or high) schools. While others have what you're asking about: an additional level that separates out adolescents into their own space between elementary and high school. Known as "middle" school (common in NY, which also has one-room schoolhouses serving K-12), "Intermediate", or "Junior High" (a term that has mostly faded away in district naming practices), the structure emerged in the early to mid 20th century, along with a shift in how Americans think about childhood.
The idea that there is a transitional stage between childhood and adulthood - what we now think of adolescence - emerged as a concept in the late 1800's. The notion was championed in America by G. Stanley Hall, first president of the American Psychological Association and all together unpleasant man, even by the standards of his era. He firmly, and loudly, believed that children were irrational, illogical, and undeserving of respect as full human beings. Beyond that, he lectured and wrote about a sort of hierarchy of childhood. Children of color with disabilities were at the bottom, then white children with disabilities, then non-disabled girls of color, etc., with white boys as the top. He believed that children in the middle of this hierarchy required extensive work and patience to be prepared to join society and saw education as a way to accomplish this.
Part of his advocacy was to persuade schoolmen (aka school administrators, men who ran schools, mostly white) to separate children by race, gender, and age. A great deal of his advocacy fell on willing ears - separating by race was a no-brainer in a country that kept Black adults and children in slavery for centuries. Race-segregated schools were the norm in the early 1900's. Separation by gender made a certain degree of sense to educators but didn't catch on at a systemic level in the way that segregation by race did. (Evidence of weak gender-based segregation efforts can be seen on schools, especially high schools built as a result of the WPA projects during the New Deal era, with separate entrances for boys and girls.) Age, though, caught on in a way that we've had a hard time shaking.
Prior to the 1850's or so in America, biological age was a loose frame for stages of life. "Youth" was used to refer to people who were seven or thirty. "Boy", likewise, had different meanings, depending on how it was used and who it was used by and about and "spinster" was used to describe unmarried women in their early 20's in certain contexts (Chudacoff, 1989.) Children didn't start formal education when they turned 5 - birthdays weren't a marker in the way they are now. Instead, formal education was dependent on parents' ability to pay tuition (if necessary), the child's ability to feed themselves and see to their basic bodily functions without assistance, and nature of schooling near where they lived. Younger children were more likely to attend school in the spring and summer, while older children typically went in the winter. This was more a function of who was needed when at home and the consequences of walking to school. There were social norms for people at different ages but they didn't have the strict demarcation points that we have today.
The 1850's saw the rise of school boards who began to set policies around who could and could not attend school. In most cases, they set the starting age at 6 - assuming by this point, a child could go half a day without needing the restroom as few schools had them, could feed themselves, take care of their own body, follow directions and had basic literacy skills (their name, numbers, letters). By the 1870's, attending school was increasingly common for children, especially in urban settings. High schools, though still relatively rare, often set the bar for admission at 12. New York State required children to sit for and pass an exam (known as the Regents Exams) before they could attend a public NYS high school.
Textbook publishers followed suit, releasing versions for "young" readers and "developed" readers. Sunday school began to separate into different levels, with guides for "infant", "elementary", "Scripture", and "senior." These, however, were based less on age and more on competency - children moved through classes as their understanding developed. As social groups emerged, such as the YMCA and Scouts, they typically offered different programs based on children's ages and would enroll or refuse children based on their date of birth.
There are a few additional bricks in the foundation of middle schools that are worth noting. In 1874, Henry Bergh, the founder of the American Society for the Prevention of Cruelty to Animals (ASPCA) took the case of an eight-year-old girl before a judge. The girl's social worker had found the child, starved and beaten by her foster parents, and was unable to get the girl help. The social worker turned to the ASPCA and Bergh agreed to take the case, and was able to persuade the judge to terminate the parents' rights. By 1900, there were over 250 societies dedicating to the safety and well-being of children. Their services typically ended when a young person turned 17 or 18 (or the child became a mother.) In 1888, the American Pediatric Society was formed and included doctors who studied and committed to serving children - recognizing their medical needs as specific to childhood and ending a history of seeing children's bodies as miniature adult bodies.
Which leads to children's brains. There was incredible amount of speculation in the early 1900's about what happened between children's ears. Many early advocates of "child study" believed too much schooling could harm a child and pushed for days no longer than 6 or 7 hours. Although Hall died in 1924, his work lived on in an increased educational, medical, and social focus on the the transition from child to adult - puberty and adolescents. It was his belief that something transformational happens when a child enters puberty and as such, they should be segregated from older and younger students. As schools merged to create districts, this notion of extra time and space to transition into high school, rather than making a jump from grammar school to high school, seemed utterly reasonable and doable. This gave rise to the notion of an intermediate, or middle, school. The look of middle school varies by state and district but generally, they have fewer students than elementary schools and fewer class periods than high school. There's often an advisory period for social emotional issues and furniture that's neither small or adult-sized.
By the mid-1900's, this extra stage was common, though not universal, in American schools.
Chudacoff, H. P. (1992). How old are you?: Age consciousness in American culture. Princeton University Press.