Scientists at the University of Edinburgh looked at the effects of paracetamol and ibuprofen on samples of human fetal testes and ovaries.
They found similar effects using several different experimental approaches, including lab tests on human tissue samples and animal studies.
Human tissues exposed to either drug for one week in a dish had reduced numbers of cells that give rise to sperm and eggs, called germ cells.
Girls produce all of their eggs in the womb, so if they are born with a reduced number it could lead to an early menopause.
The team also tested the effects of painkiller treatment on mice that carried grafts of human fetal testicular tissue. These grafts have been shown to mimic how the testes grow and function during development in the womb.
After just one day of treatment with paracetamol, the number of sperm-producing cells had dropped by 17 per cent. After a week of drug treatment, there were almost one third fewer cells.
The scientists found that exposure to paracetamol or ibuprofen triggers mechanisms in the cell that make changes in the structure of DNA, called epigenetic marks.
These marks can be inherited, helping to explain how the effects of painkillers on fertility may be passed on to future generations.
The study, published in Environmental Health Perspectives, was funded by the Medical Research Council, Wellcome and the British Society of Paediatric Endocrinology and Diabetes.
Dr Rod Mitchell, who led the research at the University of Edinburgh, said: "We would encourage women to think carefully before taking painkillers in pregnancy and to follow existing guidelines - taking the lowest possible dose for the shortest time possible."