This paper proposes an approach for the optimal operation of electrified railways by balancing energy flows among energy exchange with the traditional electrical grid, energy consumption by accelerating trains, energy production from decelerating trains, energy from renewable energy resources (RERs) such as wind and solar photovoltaic (PV) energy systems, and energy storage systems. The objective function considered in this work is the minimization of total operating cost of electrified railway system consisting of cost of power generation from the external power system, cost of power obtained from RERs such as wind and solar PV sources, cost of power from storage systems such as battery storage and supercapacitors, and the income obtained by selling excess power back to the main electrical grid. This problem is formulated as an AC optimal power flow problem subjected to various equality and inequality constraints. In this work, the probability distribution functions (PDFs) are used to the uncertainties related to wind and solar PV powers. The proposed optimization problem is solved by using CONOPT solver of generalized algebraic modeling system (GAMS) software, which is a powerful and efficient optimization tool. The simulation results obtained with GAMS/CONOPT solver are also compared with meta-heuristic based differential evolution algorithm (DEA).