Diabetes mellitus (DM) is a common chronic condition that mainly affects older adults. It's important to identify it early to prevent complications. Machine learning is essential for early detection of DM. This article introduces a new method for detecting DM using a random forest ensemble within an optimized framework. The optimized forest framework depends on finding the best DM features, which are identified using the binary multineighborhood artificial bee colony (BMNABC) technique. During preprocessing, the BMNABC algorithm efficiently identifies important features and then inputs them into the random forest within the optimized forest framework for accurate classification. Five modern DM datasets were used to validate the suggested model. The comparison of the proposed model with other leading models revealed significant insights. The BMNABC + ODF(RFE) model demonstrated exceptional proficiency in detecting diabetes mellitus (DM) across various datasets. It achieved an accuracy of 96.36% and a sensitivity of 99.95% on the merged dataset (130 US and PIMA images). The Iranian Ministry of Health dataset showed an accuracy of 97.28% and a sensitivity of 97.12%. In the Sylhet Diabetes Hospital dataset, the accuracy and sensitivity were 96.81% and 98.07% respectively. However, on the PIMA dataset, the model displayed a nuanced performance, with an accuracy of 77.21% and a sensitivity of 68.83%. Lastly, on the questionnaire dataset, the BMNABC + ODF(RFE) model achieved an accuracy of 96.43% and a sensitivity of 97.15%. These findings emphasize the model's ability to adapt and perform effectively in different clinical environments, outperforming other models in terms of accuracy and sensitivity in detecting DM.