Capitalism grew out of European feudalism. Till around the 12th century, less than 5% of the population of Europe lived in towns. Most of the people lived in rural areas. Skilled workers lived in the city but received their keep from feudal lords rather than a real wage, and the farmers were essentially serfs for landed nobles. It took the Black Plague, to shake up the feudal system significantly.
By killing scores of people in both town and countryside, the various plagues of the Dark Ages actually created a labor shortage. Many feudal lord did not want to hire poor people. Poor people started producing goods for everyone’s needs and that's how capitalism came in.
Capitalism developed to fill a need for the poor people of England who were starting to outgrow the feudal society. There was only so much land to be worked and there was a surplus of workers. These workers began to move to the cities looking for work.
The few factories that did exist in the cities existed primarily to produce goods for the upper classes. Factory owners did not want to hire poor people. So, some of the poor people innovated and began to produce goods for everyone’s needs. This was the origin of capitalism – the beginning of mass production.