This dates from the 1700s. Prior to that, children tended to live in close quarters with adults in small rooms/houses (a private room for a child would have been virtually unheard of unless the family was insanely wealthy), and society was much less stratified into different age groups - so much so that many historians have argued that people didn't really have a concept of "childhood" that was analogous to ours.
This isn't to say that babies didn't nurse and need their nappies changed, or that children didn't play; it simply means that they didn't spend all their time playing and learning and being doted on. In many respects, children were just like the other members of a household or community, but ... small. They were still expected to work, and they weren't sheltered and protected with quite the same intensity that we protect and shelter children. They would have known quite a lot about sex, and depending on when/where we're talking about, a lot of children probably witnessed a fair amount of sexual activity as they were growing up.
This changes, as I mentioned, in the 1700s. There are competing theories as to why:
Changes Religious Doctrine: Some historians argue that our perception of children as sexually innocent - and our belief that they should be kept that way - originated in theology. In this view, a theological debate over the notion of original sin (the idea that humans are inherently wicked until they are saved, and are born that way) led to the development of a new view of children as inherently innocent during the mid-1700s. According to historian Steven Mintz, religious liberals rejected notions of original sin in the mid-1700s in favor of a new view of babies and children as "malleable creatures who embodied virtue, innocence, wholeness, and purity at birth." Obviously, adults who thought of children this way wanted to keep them that way, and would have worked hard to preserve their sexual innocence.
The Enlightenment: The same shift from a view of children as "filthy bundles of original sin" to a view of children as inherently innocent can also be found in the ideas of enlightenment philosophers like John Locke and Jean Jacques Rousseau. Rather than making a religious argument, though, these philosophers suggested that children were "blank slates" (tabula rasa) by nature - that children were born more or less innocent at birth, and that their experiences and environment shaped their character. Adults who accepted this view started to worry about what impact childhood experiences would have on future generations, or on their own children's lives. Both they and philosophers like Rousseau were big on the idea of fostering virtue - which required that children be taught good self-control and not become too absorbed in pleasure-seeking. The fear of masturbation was particularly pronounced here - there's a famous work from the early 1700s called Onania: the Heinous Sin of Self-Pollution, and All its Frightful Consequences, in Both Sexes Considered, which warned that masturbation caused a whole range of different physical, spiritual, and psychological afflictions. The book was a raging best seller that went through tons of different editions during the eighteenth century. Another example is Rousseau's book Emile: On Education (1762). It was supposed to be about educating the perfect citizen, but it placed a great deal of emphasis on the need to keep children from masturbating lest they lose their inherent goodness and capacity for rational thought.
Broader Economic/Demographic Changes: European economies and daily life became way more complex than it had been in the past late seventeenth and early eighteenth century. More and more people lived in larger, more chaotic settlements and towns, and the economy was becoming increasingly complex. Parents who were members of the emerging middle class during this period began to realize that their children would need a prolonged period of skills training and education if they were to be successful in life - and some historians have argued that this growing attention to training children for success played an important role in convincing adults that children needed to be sheltered and protected. Basically, if little Johnny was going to learn a trade or excel in business, it wouldn't do to have him telling/hearing too many bawdy jokes, or for him to develop too keen an interest in sex before he had completed his education & training.
As is usually the case when historians have offered multiple, overlapping answers to a question like this, the "real" answer is most likely "all three of these factors played an important role, and none of them is a sufficient explanation in itself." The religious explanation seems to be more in-fashion lately though.
Sources:
Colin Heywood, A History of Childhood
Steven Mintz, Huck's Raft
Hugh Cunningham, Childhood and Western Society Since 1500