Both pain and mental health conditions are common among young people. They often co-occur, but we wanted to investigate further whether it is pain (abdominal pain, headache, musculoskeletal pain, menstrual pain) that precedes mental health conditions (depression, anxiety, stress, phobia) or whether it is the other way around, mental health conditions that precedes pain. Using electronic health records - the Skåne Healthcare Register - we identified and followed young people aged 7-18 over a 13-year period and tracked all their registered diagnoses. Using Poisson regression, we analyzed the incidence rate ratio (IRR) of being diagnosed with mental health conditions after an initial diagnosis of pain and vice versa the IRR of being diagnosed with pain after an initial diagnosis of a mental health condition.Among individuals with pain, 12 054 (23%) later received a diagnosis of a mental health condition. The IRR for a mental health condition after pain was 2.86 (95% CI 2.78-2.94) compared to not having pain, adjusted for age, sex and prior healthcare consultations. Among individuals with mental health conditions, 3688 (17%) later received a diagnosis of pain. The adjusted IRR was 1.57 (95% CI=1.52-1.63). Compared to boys, girls had consistently higher estimates and the same was found for the younger individuals compared to the older ones.Individuals with pain have a threefold increased risk of developing mental health conditions while the risk of developing pain after mental health conditions was lower although still elevated compared to young people seeking care regardless of cause. PerspectiveYoung people with pain have a threefold increased risk of developing mental health conditions while the reverse risk is lower but still elevated compared to young people without these conditions. Healthcare professionals must recognize the interplay between pain and mental health in young patients when diagnosing and planning treatment. Data AvailabilityThe data included in this study are stored within the Skåne county council. To access similar data, The Swedish National Board of Health and welfare and The Skåne county council can provide information about how to apply for access.