Gibraltar is a British Overseas Territory. It is under the jurisdiction and sovereignty of the UK. It is the only BOT with EU membership, which it holds as a dependent territory of the UK. This is therefore not comparable to the situation with French overseas departments, which are almost the same status as metropolitan (geographically European) departments.
Gibraltar voted 96% in favour of remaining within the EU. A few years ago, it voted even more overwhelmingly (98%) to reject joint UK-Spanish sovereignty. So, assuming no change in its sovereignty or status as a BOT, is there any legal mechanism that would allow Gibraltar to remain in the EU when the UK leaves?
I would assume not, but it's not clear to me why Gibraltar has EU membership in the first place when other BOTs don't, so I know I'm missing something. The current situation also proves that BOTs don't automatically inherit the membership status of their 'parent' state but that there is some flexibility to determine this on a case-by-base basis.