The rapid proliferation of Internet of Things (IoT) devices has fundamentally transformed how data is collected, processed, and utilized across various sectors, includ ing healthcare, smart cities, and industrial automation. However, this massive expansion has introduced unprecedented security vulnerabilities, primarily due to the heterogeneous nature and resource constraints of IoT devices. This chapter explores the critical domain of anomaly detection and intrusion prevention within IoT networks. We provide a com prehensive analysis of contemporary machine learning and deep learning methodologies designed to identify and mitigate malicious activities. The chapter introduces a robust, scalable framework leveraging an ensemble approach that combines Random Forest and XGBoost algorithms to enhance detection accuracy while minimizing false positives. Uti lizing the benchmark NSL-KDD dataset, our extensive simulation results demonstrate that the proposed ensemble model achieves an exceptional accuracy of 99.60%, outper forming traditional standalone models. Furthermore, we discuss the practical implemen tation of Intrusion Prevention Systems (IPS) tailored for resource-constrained IoT envi ronments. The detailed results and discussions section provides an in-depth evaluation of model performance, scalability, and computational efficiency, offering valuable insights for researchers and practitioners aiming to secure next-generation IoT infrastructures.