The title bar isn't always the only spot to that can be used to drag the window, (these days some windows can be dragged by parts of their background, but it's rare) but it's the oldest and most established and common convention. It originated in the early WIMP UIs, the Smalltalk systems from the 70s and 80s.

If you study the way the above windows were designed you'll see there really isn't any other affordance on a window for dragging it other than the title bar - there's no unused window background that could be grabbed (remember screen real estate was much more expensive back then). Soon the title bar became extended across the whole width of the window, making the window rectangular shaped, providing more space to grab and made screen updates computationally more efficient. And making the title bar full width allowed it to hold buttons that act as window manipulation shortcuts.
This turned out to be a very learnable, usable system and was copied by many different UIs (Mac, Windows, etc.). There wasn't any good reason to go away from this convention. It wasn't broke so there was no need to fix it, and it still survives today.