Every Android phone has a "recovery“ partition which is used for performing factory resets using an OEM’s preloaded tools. However, this partition can be modified in order to replace the default tools by third-party recovery tools such as TWRP or CWM.
These recoveries are (unlike the stock ones) capable of modifying all the internal system partitions of your phone or tablet (they need this capability in order to flash custom firmware).
TWRP even comes with a built-in file manager with unlimited root access so you can modify, add, or delete any system files manually.
The most important thing is that TWRP has a working MTP connection and ADB enabled, which allows us to extract almost all data stored on your device and create physical images from them.
By default, you can flash TWRP (or another recovery) image files to almost any device with an unlocked bootloader (a locked bootloader prevents users from sideloading any software to system partitions, so in order to flash anything on such device, you need to unlock the bootloader first).
You can do so by using the "Fastboot“ mode which allows the user to flash various system partitions including recovery. You can control your phone in the “fastboot” mode using Windows or Linux command line (similar to ADB).
The universal commands for flashing recovery images while in the “fastboot” mode are:
fastboot flash recovery "xxx.img" – flash certain recovery image
fastboot oem unlock – unlocks bootloader on supported devices
fastboot boot "xxx.img" – boots straight from IMG file
fastboot reboot recovery – reboots to recovery
fastboot reboot – reboots the device
Samsung phones are different. They have "Download mode" instead of regular fastboot. Therefore, they can be controlled using Odin, a tool for flashing software developed by Samsung, or its open-source alternative called Heimdall.
Samsung phones also don’t have their own recovery partition like other Android smartphones. Instead, they have a special ramdisk (a small IMG partition mounted by the kernel before and while booting the system) as a part of "boot.img" dedicated specifically for recovery.