Using pp collision data, corresponding to an integrated luminosity of $1.0 mathrm{fb}^{-1}$, collected by LHCb in 2011 at a center-of-mass energy of 7 TeV, we report the measurement of direct CP violation in $B^0_s to K^-pi^+$ decays, $A_{C/!P}(B^0_s rightarrow K^- pi^+)=0.27 pm 0.04,mathrm{(stat)} pm 0.01,mathrm{(syst)}$, with significance exceeding five standard deviations. This is the first observation of CP violation in the decays of $B^0_s$ mesons. Furthermore, we provide an improved determination of direct CP violation in $B^0 to K^+pi^-$ decays, $A_{C/!P}(B^0rightarrow K^+ pi^-)=-0.080 pm 0.007,mathrm{(stat)} pm 0.003,mathrm{(syst)}$, which is the most precise measurement of this quantity to date.