It is polite when someone says ''yes madam'' ( or yes ma'am) to a lady of higher authority. It's impolite to address them in their first names ro surnames. However in some occasions, it is allowed to address the lady as Mrs.(her name).
In yourcontext, if I read it right, your teacher called you ''ma'am''? In which case, she only did from a humoristic point of view.
Traditionally, madam is for married women while unmarried women are miss.
Savvy