In real-time manner, the deployment of 5G network is an essential role in Internet of Things (IoT) by facilitating with massive Machine-Type Communications (mMTC). This advancement in IoT technology becomes crucial for enabling a wide variety of applications like intelligent manufacturing, smart energy grids, and development of smart cities. The most commonly known attacks in IoT are Distributed Denial of Service (DDoS) that causes huge traffic flow into the large number of devices and misleading transmission of data packets. However, the defensive measures in existing approaches are largely ineffective. Among various cyber threats, the defense against DDoS attacks is particularly challenging due to their dynamic nature. Also, the accurate detection of DDoS is a quite challenging in the traditional methods of 5G-based IoT networks. In order to resolve these issues, the research work implements an effective model using optimal routing to safeguard the 5G-based IoT networks. The advancement of these model helps to mitigate the DDoS attack that offer effective data transmission. Initially, the input data is accumulated from IoT network. Further, the DDoS attack detection is performed via the developed Adaptive Cascaded Gated Recurrent Unit (ACGRU), in which the parameters are optimally tuned using Exploration or Exploitation-based Pine Cone Optimization (EEPCO) algorithm. The detected node is further mitigated for an effective and secured data transfer. In order to achieve that, the optimal routing is done by using EEPCO. To attain better optimal routing, the objective functions are formulated with constraints like trust, energy and throughput. Throughout the experimental analysis, the diverse positive measures like accuracy, specificity, sensitivity, and precision are validated to show the effective outcome as 94.05, 94.69, 93.39 and 93.68 while detecting the attacks. On considering statistical analysis, the performance of the developed EEPCO algorithm attains 5.4%, 7.3%, 33.8% and 11.5% enhanced than DSO, EVO, GFROA and PCOA in terms of median. Therefore, the security of the IoT network is improved by optimal routing and helpful for transmitting the data without any delay.