I believe it is all part of a tradition passed down to us from the days of Haydn. It is the 'Trio'.
Yes, I know a 'trio' usually means a group of three - three musicians, for example.
But there is another use of the word 'Trio' in connection with music and it dates from the way pieces were structured by the classical composers of the mid-Eighteenth Century.
Symphonies and string quartets by the likes of Haydn, Mozart and Beethoven were made up of several units of music, spread over a number of movements.
The 'Trio', which often came within a 'Minuet' movement, was often provided as a contrast to another theme that was played before and after it.
Why it was called a 'Trio' is somewhat obscure, but it seems likely that, originally at least, it really did involve some kind of three-part harmonizing. (You can certainly find that in Haydn string quartets.)
Move forward to the late Nineteenth Century and we find that composers of light or semi-classical music still considered it proper to create pieces that included four or five separate themes, or parts. By analogy, they thought they too should have a section called the Trio; and often it appeared as a somewhat grand but simple melody (usually 16 or 32 bars). Just as the classical composers had done, they often switched to a different key for the Trio. And, just as in classical music, they sometimes indicated that the musicians were expected to go back to the opening theme and play that again AFTER the Trio.
This idea of including Trios was immensely popular in Brass Band music of the Nineteenth Century. Think of those great marches, in many of which there is a theme called the 'Trio'. Sometimes it is quite grandiose.
So it's hardly surprising that the Trio found its way into early jazz - and that it's still there today, though I doubt whether any of our current musicians ever consciously think about it.
Look at the original sheet music of some of our jazz classics. This is the final section of Deep Henderson. I have highlighted where the Trio begins.
The same happens in Maple Leaf Rag. Here the switch is from the key of Ab (for the earlier themes) to Db for the Trio.
A few more examples: