The atomic environments involved in the magnetostriction effect in CoFe$_2$O$_4$ and La$_{0.5}$Sr$_{0.5}$CoO$_3$ polycrystalline samples have been identified by differential extended x-ray absorption fine structure (DiffEXAFS) spectroscopy. We demonstrate that cobalt atoms at octahedral sites are responsible for their magnetostriction. The analysis of DiffEXAFS data indicates that the local-site magnetostrictive strains of Co atoms are reversed in these two oxides, in agreement with the macroscopic magnetostriction. For the CoFe$_2$O$_4$ spinel, a large negative strain along the (100) direction has been determined for the CoO$_6$ octahedron causing a tetragonal contraction in contrast with the La$_{0.5}$Sr$_{0.5}$CoO$_3$ perovskite, where a positive moderate strain along the (100) direction was found resulting in a tetragonal expansion. The different local-site magnetostriction is understood in terms of the different valence and spin state of the Co atoms for the two oxides. The macroscopicmagnetostriction would be explained then by the relative change in volume, either contraction in CoFe$_2$O$_4$ or expansion in La$_{0.5}$Sr$_{0.5}$CoO$_3$, when the tetragonal axis of the Co site is reoriented under an externally applied magnetic field.