Mental illness is on the rise globally, significantly impacting the lives of individuals across diverse communities. Social media serves as an increasingly vital platform for millions of individuals with mental illnesses to express themselves and look for help coping with their illnesses. A more thorough comprehension of mental conditions could be gained through text analysis of social media posts. It can also aid in the early detection of such illnesses. Artificial intelligence (AI) techniques are currently being utilized to assist professionals in mental health in making decisions based on individuals' data, including that gathered through social media platforms. This research introduces an AI-driven framework for identifying mental health disorders through unstructured user-generated content on the Reddit platform. The framework capitalizes on the Bidirectional Encoder Representations from Transformers (BERT) deep learning technique through a comparative study of pre-trained BERT models such as BERT-base, DistilBERT, ROBERTa, and AlBERT to classify diverse mental health conditions. The Kim and Low datasets utilized in this study were obtained from prior research. This study performed pre-processing on the data using natural language processing (NLP) techniques, which enhanced the clarity and quality of the text-based content for mental health classification. In addition, the presented framework addresses the challenging issue of imbalanced data commonly encountered in mental health detection and diagnosis through a combination of pre-trained BERT models, weighted cross-entropy, and focal Losses. The results show that the framework achieved a 91% accuracy for classifying six mental disorders in the Kim dataset and 87.85% for 11 mental issues in the Low dataset. Thus, this approach holds great potential to advance mental health diagnosis and intervention, offering a promising avenue for real-world application.