In movies, mostly around military personnel, female officers are sometimes addressed as "Sir" (Sometimes also followed by a "Um, ma'am, sorry..."). What would be the correct usage here if not using military rank? Are both" sir" and "ma'am/madam" correct?
Sir, although originally signifying knighthood, has evolved into a modern honorific which is simply used to address a man in a polite/respectful way.
Madam is the female equivalent, which is the full form of the contraction ma'am. It originated from the French term madame, which means "my lady".
Both are used to address someone formally.
Addressing a woman as sir is not really ever appropriate, because a knighted woman would have dame as her title and would be addressed as "Lady".
In the military it is question of regulation and the regulation is driven by tradition. For example, in the Hungarian army we use a very strange mix which contains both gender. Outside the military I would use "ma'am/madam".