This rule is derived from the fact that, perhaps coincidentally, for the most common chemical elements in neutral organic compounds (hydrogen, carbon, nitrogen, oxygen, silicon, phosphorus, sulfur, and the halogens), elements with even numbered nominal masses form even numbers of covalent bonds, while elements with odd numbered nominal masses form odd numbers of covalent bonds, with the exception of nitrogen, which has a nominal (or integer) mass of 14, but has a valency of 3. 
The nitrogen rule is only true for neutral structures in which all of the atoms in the molecule have a number of covalent bonds equal to their standard valency (counting each sigma bond and pi bond as a separate covalent bond for the purposes of the calculation).  Therefore, the rule is typically only applied to the molecular ion signal in the mass spectrum.
Mass spectrometry generally operates by measuring the mass of ions.  If the measured ion is generated by creating or breaking a single covalent bond (such as protonating an amine to form an ammonium center or removing a hydride from a molecule to leave a positively charged ion) then the nitrogen rule becomes reversed (odd numbered masses indicate even numbers of nitrogens and vice versa).  However, for each consecutive covalent bond that is broken or formed, the nitrogen rule again reverses.
Therefore, a more rigorous definition of the nitrogen rule for organic compounds containing exclusively hydrogen, carbon, nitrogen, oxygen, silicon, phosphorus, sulfur, and the halogens would be as follows:
An even nominal mass indicates that a net even number of covalent bonds have been broken or formed and an even number of nitrogen atoms are present, or that a net odd number of covalent bonds have been broken or formed and an odd number of nitrogen atoms are present.  An odd nominal mass indicates that a net even number of covalent bonds have been broken or formed and an odd number of nitrogen atoms are present, or that a net odd number of covalent bonds have been broken or formed and an even number of nitrogen atoms are present.
Inorganic molecules do not necessarily follow the rule. For example, the nitrogen oxides NO and NO have an odd number of nitrogens but even masses of 30 and 46, respectively.