Depressive disorder (DD) and generalized anxiety disorder (GAD), two prominent mental health conditions, are commonly diagnosed using subjective methods such as scales and interviews. Previous research indicated that machine learning (ML) can enhance our understanding of their underlying mechanisms. This study seeks to investigate the mechanisms of DD, GAD, and healthy controls (HC) while constructing a diagnostic framework for triple classifications. Specifically, the experiment involved collecting electroencephalogram (EEG) signals from 42 DD patients, 45 GAD patients, and 38 HC adults. The Phase Lag Index (PLI) was employed to quantify brain functional connectivity and analyze differences in functional connectivity among three groups. This study also explored the impact of time window feature computations on classification performance, including the XGBoost, CatBoost, LightGBM, and ensemble models. In order to enhance classification performance, a feature optimization algorithm based on Autogluon-Tabular was proposed. The results indicate that a 12 s time window provides optimal classification performance for the three groups, achieving the highest accuracy of 97.33% with the ensemble model. The analysis further reveals a significant reorganization of the brain, with the most pronounced changes observed in the frontal lobe and beta rhythm. These findings support the hypothesis of abnormal brain functional connectivity in DD and GAD, contributing valuable insights into the neural mechanisms underlying DD and GAD.