DC microgrids can be operated under a hierarchical control strategy, and it needs a communication-based layer. The implementation of digital controllers and the communication infrastructure can make a dc microgrid vulnerable to cyber-attacks. This article introduces an approach based on Artificial Intelligence (AI) to detect and mitigate cyber-attacks in a dc microgrid. The proposed method is based on the artificial neural network (ANN), which can be categorized as an AI-based method. The proposed application implements an ANN to detect and mitigate false data injection attacks (FDIAs). FDIAs try to inject false data into the system to affect the control application of the dc microgrid, and it can shut down the dc microgrid. The proposed method can calculate the value of the false data, and it can detect and remove the attack simultaneously. The proposed method is tested in a MATLAB/Simulink environment. Also, to have more accurate results, the introduced approach is examined under different conditions and cyber/physical disturbances (e.g., communication delay, noise, plug-and-play of additional units, and time-varying FDIAs). Besides, a comparison is considered to evaluate the effectiveness of the proposed strategy. The obtained results can conclusively prove the effectiveness, accuracy, and authenticity of the proposed method to successfully detect the FDIAs and remove the cyber-attack.