PIC pin state change reset the PIC
I would guess, when you connect the +5V_SEC to +5V, there is a transient dip on +5V.  That is perceived by the PIC as a brownout.  The dip happens because the capacitors connected to +5V_SEC suddenly charge.  You can probably see the dip with an oscilloscope.

[I've seen that sort of behavior before.  I'll see if I can find the old oscilloscope screenshot.]

On a different note.  Add a pull-down resistor from the gate of Q1 to ground.  That will ensure that Q1 will not turn on RB7 is high impedance.