Objective This cohort study investigated the relationship between serum magnesium levels and the incidence of acute kidney injury (AKI) in patients with acute pancreatitis (AP). Methods We analyzed data from the Medical Information Mart for Intensive Care IV (MIMIC-IV) database, focusing on adult AP patients (≥18 years old) with ICU stays longer than 24 h. Univariable logistic regression was used to identify key covariates. Both univariate and multivariable logistic regression models were used to evaluate the link between serum magnesium levels and AKI occurrence. A restricted cubic spline (RCS) was used to visualize the associations, and odds ratios (ORs) with 95% confidence intervals (CIs) were calculated. Results During hospitalization, 455 (69.1%) participants experienced AKI. After adjusting for relevant factors, higher serum magnesium concentrations were significantly associated with AKI (OR: 2.247, 95% CI: 1.234–4.091; p = 0.008). Magnesium levels between 1.6–2.6 mg/dL (OR: 1.872, 95% CI: 1.105–1.174, p = 0.020) and ≥2.6 mg/dL (OR: 3.851, 95% CI: 1.08–13.736, p = 0.038) were linked to increased AKI occurrence. The RCS curve indicated a nonlinear relationship, suggesting a potential threshold effect between serum magnesium and AKI risk. Conclusions Higher serum magnesium levels were found to be associated with AKI in ICU patients with acute pancreatitis. Further studies are needed to explore this relationship.