People who take prescription opioids, which are used for treating pain, for longer than a month may have an increased risk of developing depression, according to a new study.

Pain itself can also lead to depression, but in the study, the link between depression and opioid use held even when the researchers accounted for the potential contribution of pain to depression, according to the study.

Therefore, if people who are taking opioids for pain notice they have been feeling depressed, both they and their doctors should be aware that the use of the drugs, and not just the pain, may be a potential source of the depressed mood, the researchers said.

"We really did rigorous control for pain, and we feel strongly that these results are independent of the known contribution of pain to depression," said study author Jeffrey Scherrer, an associate professor of family and community medicine at Saint Louis University in Missouri. [7 Ways Depression Differs in Men and Women]

In the study, the researchers looked at data from three large groups of people who started taking opioids around the time the study started. The first group had nearly 71,000 people; the second had nearly 14,000 people; and the third had nearly 23,000 people. The ages of the people in the study ranged from 18 to 80 years.

The people did not have depression at the start of the study. The researchers followed up with the participants for seven to 10 years, depending on which group the people were in.

The researchers found that 12 percent of the nearly 71,000 people in the first group, 9 percent of the nearly 14,000 people in the second group and 11 percent of the nearly 23,000 people in the third group had developed depression during this time.

They also found that the longer the people took the opioids, the greater their risk of depression was. For example, in the group with almost 71,000 people, 11.6 percent of those who used opioids for one day to one month developed depression, compared with 13.6 percent of those who used opioids for one to three months and 14.4 percent of those who used the drugs for longer than three months.

And in the group with nearly 14,000 people, 8.4 percent of those who used opioids for one day to one month developed depression, compared with 10.6 percent of those who used opioids for one to three months and 19 percent of those who used the drugs for longer than three months.

The opioids included in the study were codeine, fentanyl, hydrocodone, hydromorphone, levorphanol, meperidine, oxycodone, oxymorphone, morphine and pentazocine.

Many experts say that opioids are overprescribed in the U.S. There were 259 million prescriptions written for opioids in 2012, which is more than enough to give every American adult their own bottle of pills, according to the Centers for Disease Control and Prevention. In 2014, prescription pain relievers were linked to nearly 19,000 overdose deaths.

It is not clear why the long-term use of opioids is linked to a greater risk of depression, but it may have something to do with lowered levels of testosterone, Scherrer said.

"We know that chronic opioid use leads to low testosterone in males and females, and that is known to be related to poor mood," he told Live Science. A behavioral pathway is also possible, as "some patients may start to lose control and develop early symptoms of [opioid] misuse, which is known to be related to depression," he noted.

The study was published online Jan. 11 in the journal Annals of Family Medicine.

Copyright 2016 LiveScience, a Purch company. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.