In recent years, the wide adoption of the modern Internet of Things (IoT) paradigm has led to the invention of smart cities. Smart cities operate in real-world time to promote ease and quality of life in urban cities. The network traffic of a smart city via IoT systems is growing exponentially and introducing new cybersecurity challenges since these IoT devices are being connected to sensors that are directly connected to massive cloud servers. In order to mitigate these cyberattacks, the developers need to enhance new techniques for detecting infected IoT devices. In this paper, to address the IoT cybersecurity threats in a smart city, we propose an Anomaly Detection-IoT (AD-IoT) system, which is an intelligent anomaly detection based on Random Forest machine learning algorithm. The proposed solution can effectively detect compromised IoT devices at distributed fog nodes. To evaluate our model, we utilized modern dataset to illustrate the model's accuracy. Our findings show that the AD-IoT can effectively achieve highest classification accuracy of 99.34% with lowest false positive rate.