Serum levels of bilirubin, a strong antioxidant, may influence cancer risk. We aimed to assess the association between serum bilirubin levels and cancer risk. Data were retrieved from 10-year electronic medical records at Kyushu University Hospital (Japan) for patients aged 20 to 69 years old. The associations of baseline bilirubin levels with cancer risk (lung, colon, breast, prostate, and cervical) were evaluated using a gradient boosting decision tree (GBDT) model, a machine learning algorithm, and Cox proportional hazard regression model, adjusted for age, smoking, body mass index, and diabetes. The number of study subjects was 29,080. Median follow-up time was 4.7 years. GBDT models illustrated that baseline bilirubin levels were negatively and non-linearly associated with the risk of lung (men), colon, and cervical cancer. In contrast, a U-shaped association was observed for breast and prostate cancer. Cox hazard regression analyses confirmed that baseline bilirubin levels (< 1.2 mg/dL) were negatively associated with lung cancer risk in men (HR = 0.474, 95% CI 0.271–0.828, P = 0.009) and cervical cancer risk (HR = 0.365, 95% CI 0.136–0.977, P = 0.045). Additionally, low bilirubin levels (< 0.6 mg/dL) were associated with total death (HR = 1.744, 95% CI 1.369–2.222, P < 0.001). Serum bilirubin may have a beneficial effect on the risk of some types of cancers.