The rapid development of number of connected devices exchange sensitive and personal significant data through Internet of Things (IoT)-assisted global network, attacks which are targeting security services are also enhancing every day. This paper proposed a Mayfly Optimization Algorithm (MOA) with Bidirectional Long-Short Term Memory (BiLSTM) for Intrusion Detection System (IDS) in $\text{IoT}$ . The MOA is used for feature selection which select relevant features through balancing exploration and exploitation. The BiLSTM is used for classifying network into attack and normal which process the data in both forward and backward directions, allowing them to capture dependencies between events that might be far apart in sequence. The min-max normalization is used to normalize the continuous features from NSL-KDD, ToN-IoT and UNSW-NB15 datasets. The recall, accuracy, f1-score and precision are considered to evaluate MOA-BiLSTM performance. The MOA-BiLSTM realizes accuracy of 99.25%, 89.61% and 99.35% accuracy for NSL-KDD, ToN-IoT and UNSW-NB15 datasets when compared to existing techniques such as LSTM and Deep Neural Network (DNN).