We may be running into the XY problem here.
More precisely, I want to know how does the main loop hands off the rendering to another loop that exist in the level code.
Most games don't do this. Whether you're in level 1 or level 2, the code rendering is the exact same. The only thing that changes is which objects are loaded up into the scene manager and where the current camera is looking.
Likewise, game logic doesn't necessarily change between levels, either. The actual game logic is generally identical and all that changes is which data is loaded up. If you make your game logic data-driven enough, you can even have vastly different types of levels without needing any specific code added for them.
Menus can also just be another type of level with menu objects instead of characters. Or they might be handled by an entirely different system like a third-party GUI library (which needs to be able to run at the same time as a level for pause and settings menus).
I could think of a solution but I would like to know how this is done by game developers.
They wouldn't have per-level rendering, and probably wouldn't have per-level hard-coded logic.
However, hard-coded level logic is easier and a good first step for a beginner. This is how you should start. The other answers cover the details of how that is done.
I would definitely avoid per-level hard-coded rendering, at least outside of menus.