The anisotropic nanomechanical properties of AZ31 magnesium alloy and pure Mg were measured in situ via nanoindentation of individual grains with simultaneous observations using a scanning electron microscope. Values of the nanohardness, indentation size effect, elastic modulus, and yield strength were correlated with the crystallographic orientation provided by electron backscattering diffraction and were further used to investigate the relationships between the nanomechanical properties of the materials and the work of nanoindentation.The nanohardness of AZ31 was found to be generally above that of pure Mg due to solid solution strengthening. The nanohardness of AZ31 first considerably decreased and then marginally increased, whereas the nanohardness of pure Mg steadily decreased as the angle between the hexagonal lattice c-axis of both materials and the indentation direction increased. The indentation size effect was stronger for AZ31 than for pure Mg, and its magnitude decreased as the angle between the lattice c-axis and the indentation direction increased. The AZ31 modulus remained nearly constant throughout the range of investigated orientations; the modulus of pure Mg followed a theoretical angular dependence but was generally lower than expected. The yield strength behaved in a similar manner to the nanohardness in both materials. Plots of the ratio of the nanohardness to the yield strength revealed that both materials underwent significant work hardening shortly after nanoindentation began. It was also shown that the amount of plastic deformation increased for Mg and increased or remained nearly constant for AZ31 as the angle increased. The observed orientation dependencies were interpreted as a consequence of the anisotropic activities of the dominant slip systems and extension twinning.