When can be say "mahlzeit"?
I do not know it is said only for lunch or every time you look someone who is eating. ex morning.
Mahlzeit is used as a kind of colloquial greeting among colleagues around lunchtime. It's also said if somebody burps (coll. context) or if something goes wrong ("Prost Mahlzeit"). These usages come from the outdated or religious "Ich wünsche (Dir/Euch/Ihnen) eine gesegnete Mahlzeit" (enjoy your meal).
In a formal context it means simply "meal".
If you are referring to the noun die Mahlzeit, that can be used for any meel of the day.
If you just say Mahlzeit you wish somebody a pleasant meal.
Nowadays some people even use it for greeting someone around noon, even if nobody is about to eat. Drives me nuts.