The use of opioid medicines is common in developed countries, particularly among older adults and those with mental health disorders. It is unclear if the association between mental disorders and opioid medicines is causal, or is due to reverse causality or confounding. We used a 10% random sample of the Australian Pharmaceutical Benefits Scheme (years 2012-2022) to examine the cross-sectional, case-control and longitudinal association between the dispensing of antidepressants, anxiolytics, hypnotics, antipsychotics and lithium, and opioid medicines. We used logistic regression, structural equation models (SEM), and Cox regression to analyze the data. Analyses were adjusted for age (years), sex, and number of non-psychotropic medicines dispensed during the year. The 2022 file contained 804 334 individuals aged 50 years or over (53.1% women), of whom 181 690 (22.6%) received an opioid medicine. The adjusted odds ratio of being dispensed opioid medicines was 1.44 (99% CI = 1.42-1.46) for antidepressants, 1.97 (99% CI = 1.92-2.03) for anxiolytics, 1.55 (99% CI = 1.51-1.60) for hypnotics, 1.32 (99% CI = 1.27-1.38) for antipsychotics, and 0.60 (99% CI = 0.53-0.69) for lithium. Similar associations were noticed when we compared participants who were or not dispensed opioid medicines in 2022 for exposure to psychotropic agents between 2012 and 2021. SEM confirmed that this association was not due to reverse causality. The dispensing of antidepressants was associated with increased adjusted hazard (HR) of subsequent dispensing of opioid medicines (HR = 1.29, 99% CI = 1.27-1.30). Similar associations were observed for anxiolytics, hypnotics and antipsychotics, but not lithium. The dispensing of opioid medicines is higher among older individuals exposed to antidepressants, anxiolytics, hypnotics and antipsychotics than those who are not. These associations are not due to reverse causality or study design. Preventive strategies seeking to minimise the risk of inappropriate use of opioid medicines in later life should consider targeting this high-risk population.