This paper addresses the problem of reactive power control of distributed generation (DG) units in the medium voltage (MV) distribution systems in order to maintain the system voltages within the predefined limits. It is formulated as an optimization problem which aims to minimize the reactive power changes of DG units while returning the voltage of the violated buses inside the permitted limits with taking into account the available reactive power capacity of DGs. Moreover, the thermal limit of the system branches is considered. A new load flow algorithm is used here for evaluating the system voltages. This load flow algorithm with a simple theory uses the topological structure of the network. Also, a new voltage sensitivity analysis method is presented in this paper which is also based on the topological structure of the network and independent of the network operating points. Therefore, the voltage sensitivity matrix is built simultaneously with the load flow program. The optimal reactive power changes are obtained by implementing particle swarm optimization (PSO) algorithm. Simulation results reveal that the proposed algorithm is capable of keeping the system voltages and currents within the permitted limits.