It is well known that the accuracy of simulations of combustion processes in diesel and spark ignited (SI) engines depends on the initial conditions within the cylinder at intake valve closure (IVC). Residual gas affects the engine combustion processes through its influence on charge mass, temperature and dilution. In SI engines, there is little oxygen in the residual gas, and thus the dilution effect on flame propagation is more significant than in compression ignited (CI) engines. However, in CI engines, the ignition delay depends strongly on the in-cylinder gas temperature, which is proportional to the gas temperature at IVC. Furthermore, ignition delay is significantly affected by how much oxygen is present, which is also partly determined by the residual gas fraction. Therefore, it is of extreme importance to determine residual gas concentrations accurately. In this paper, a general zero-dimensional model for calculating the residual gas fraction in reciprocating internal combustion engines is formulated. The model accounts for the back-flow of exhaust gas into the cylinder during the valve overlap period. The model constants are determined using available in-cylinder hydrocarbon measurements at different values of intake pressures, engine speeds and valve overlap timings. The predictions from the present zero-dimensional model are compared with three-dimensional simulations in which the flow fields in the cylinder, intake and exhaust ports are calculated. The effects of engine speed and fraction of constant-volume combustion which, to a certain extent, represents combustion characteristics of engines on the residual gas fraction are investigated. The present model is useful for determining the initial conditions for multi-dimensional simulations.