In this study, the challenges of incorporating PV systems and wind energy sources into smart city power grids are addressed. The fluctuating nature of renewable energy is considered, and microgrid energy management is investigated using probabilistic and deterministic assumptions. To enhance energy management, digital twin simulations are employed, providing a virtual representation of the power grid. This enables accurate analysis and prediction of system behavior in different scenarios. Digital twins help simulate the effects of load profiles on microgrids, facilitating better energy planning and decision-making. The proposed framework considers objective functions related to cost reduction, voltage profile improvement, and voltage stability enhancement. The whale optimization algorithm (WOA) is applied to solve the complex and nonlinear energy management problem, aiming to minimize costs and enhance voltage stability. Simulation results show that an optimally installed PV system and wind turbines reduce energy costs and improve overall system efficiency in smart cities. Furthermore, the objective function of WOA outperforms particle swarm optimization (PSO) and differential evolution algorithm (DEA) in achieving the desired outcomes.