Is it possible to install a custom Android ROM if the bootloader is locked? Suppose the bootloader of an Android device is locked.

Is there any way to install a custom ROM without unlocking the bootloader? If yes, then how?
Yes, however, this method will not work on every phone.

Prerequisites


Back up all data on your phone
Unknown sources enabled in security settings
Being prepared to void warranty.


Method


Download and install an on-device one-click-root root-management application on your phone (notable mentions include Kingo Root and Kingroot, both personally tried and tested to work)
Use the application to root your phone and gain root access.
Download and install an on-device partition-flashing application. (notable mentions include Rashr, Flashify or even the official TWRP app)
Download a custom recovery for your device (TWRP recommended, see here)
Use the app you downloaded to flash the recovery file you downloaded.
Reboot into recovery (device-specific)
Install any custom ROM you want


You are now rooted and have a custom recovery installed; this is an enormous security risk, especially since anyone can delete your lockscreen password from recovery mode and hence have full access to your device.

If you brick your phone you will have a very hard time getting out of such a situation because your bootloader is locked.

I have personally tried this method before on a Nexus 5 running and Lollipop 5.1.1 LMY48M and it worked.

I would recommend going the "old-fashioned" way to root your device and unlock your bootloader.