Several puzzling aspects of interplay of the experimental lattice distortion and the the magnetic properties of four narrow t2g-band perovskite oxides (YTiO3, LaTiO3, YVO3, and LaVO3) are clarified using results of first-principles electronic structure calculations. First, we derive parameters of the effective Hubbard-type Hamiltonian for the isolated t2g bands using newly developed downfolding method for the kinetic-energy part and a hybrid approach, based on the combination of the random-phase approximation and the constraint local-density approximation, for the screened Coulomb interaction part. Apart form the above-mentioned approximation, the procedure of constructing the model Hamiltonian is totally parameter-free. The results are discussed in terms of the Wannier functions localized around transition-metal sites. The obtained Hamiltonian was solved using a number of techniques, including the mean-field Hartree-Fock (HF) approximation, the second-order perturbation theory for the correlation energy, and a variational superexchange theory, which takes into account the multiplet structure of the atomic states. We argue that the crystal distortion has a profound effect not only on the values of the crystal-field (CF) splitting, but also on the behavior of transfer integrals and even the screened Coulomb interactions. Even though the CF splitting is not particularly large to quench the orbital degrees of freedom, the crystal distortion imposes a severe constraint on the form of the possible orbital states, which favor the formation of the experimentally observed magnetic structures in YTiO3, YVO3, and LaVO3 even at the level of mean-field HF approximation. It is remarkable that for all three compounds, the main results of all-electron calculations can be successfully reproduced in our minimal model derived for the isolated t2g bands. We confirm that such an agreement is only possible when the nonsphericity of the Madelung potential is explicitly included into the model. Beyond the HF approximation, the correlations effects systematically improve the agreement with the experimental data. Using the same type of approximations we could not reproduce the correct magnetic ground state of LaTiO3. However, we expect that the situation may change by systematically improving the level of approximations for dealing with the correlation effects.