On the origin of the fundamental metallicity relation and the scatter in galaxy scaling relations