The relation between metallicity, stellar mass and star formation in galaxies: an analysis of observational and model data