In this study we aimed to develop an artificial intelligence-based model for predicting postendoscopic retrograde cholangiopancreatography (ERCP) pancreatitis (PEP). We retrospectively reviewed ERCP patients at Nagoya University Hospital (NUH) and Toyota Memorial Hospital (TMH). We constructed two prediction models, a random forest (RF), one of the machine-learning algorithms, and a logistic regression (LR) model. First, we selected features of each model from 40 possible features. Then the models were trained and validated using three fold cross-validation in the NUH cohort and tested in the TMH cohort. The area under the receiver operating characteristic curve (AUROC) was used to assess model performance. Finally, using the output parameters of the RF model, we classified the patients into low-, medium-, and high-risk groups. A total of 615 patients at NUH and 544 patients at TMH were enrolled. Ten features were selected for the RF model, including albumin, creatinine, biliary tract cancer, pancreatic cancer, bile duct stone, total procedure time, pancreatic duct injection, pancreatic guidewire-assisted technique without a pancreatic stent, intraductal ultrasonography, and bile duct biopsy. In the three fold cross-validation, the RF model showed better predictive ability than the LR model (AUROC 0.821 vs. 0.660). In the test, the RF model also showed better performance (AUROC 0.770 vs. 0.663, P = 0.002). Based on the RF model, we classified the patients according to the incidence of PEP (2.9%, 10.0%, and 23.9%). We developed an RF model. Machine-learning algorithms could be powerful tools to develop accurate prediction models.