on purpose, or not on purpose hardware doesn't care about such matters.
If there has been 7V on a GPIO that, (as all CMOS type inherently drivers do) has a protection diode from the GPIO to the 3V3 lead, and another one to GND, (both obviously orientated so that no current flows through them under normal circumstances), then if you put 7V on such a GPIO current will
flow from the +7V to the +3V3 through the first of these two diodes (the other one tries to protect against negative voltages), and if you are lucky only that diode burns out, instead of all the 3V3 logic that suddenly got powered with 7V coming in through that diode. After the diode has gone up in smoke it turns into a short, and this means there will always be 3V3 on the GPIO pin afterwards.
Of course it all depends on how much current that 7V source can deliver, if its just a few hundred milliamps the diode might survive, and if its a voltage below what the lower output transistor can handle it might stay intact.
If the voltages you put onto the GPIO pins are reasonable low, (say 5V, or perhaps even 7V) all you need to do to efficiently protect the GPIO pin is to place a current limiting resistor (say 1K) in series with the GPIO, so that the current flowing into the GPIO pin is limited to a few dozen mA or so, instead of a few amperes.
Why does a battery cause an overcurrent whereas a power supply works fine?
Perhaps the power supply has an over-current protection, that limits its output current, while a battery has no such thing, if its something like a lithium ion battery it can deliver dozen of amperes for a short time. or another factor you are not aware of has happened, like touching a wire with a pin without you noticing...