The increasing number of devices in the Internet of Things (IoT) has exposed various vulnerabilities, such as BASHLITE and Mirai attacks, making it easier for cyber threats to emerge. Due to these vulnerabilities, developing innovative detection and mitigation strategies is essential. Our proposed solution is an ensemble-based weighted voting model that combines different classifiers, including Random Forest, eXtreme Gradient Boosting (XGBoost), Gradient Boosting, K-nearest neighbor (KNN), Multilayer Perceptron (MLP), and Adaptive Boosting (AdaBoost), using artificial intelligence and machine learning. We evaluated our model on the N-BaIoT dataset, a benchmark in this domain. Our results show that the weighted voting approach has exceptional accuracy, precision, recall, and F1-Score. This highlights the effectiveness of our model in classifying various attack instances within the IoT security context. Our approach performs better than other state-of-the-art methods, achieving a remarkable accuracy of 99.9955% in detecting and preventing BASHLITE and Mirai cyber-attacks on IoT devices.