Unfortunately the exact origin of the custom is lost in the earliest days of the game. Baseball historian Dan Daniel is quoted by Zander Hollander (baseball lingo, 1967) :”It probably originated as an expression of fatigue and tedium, which seems to explain why the stretch comes late in the game instead of at the halfway point.”
The earliest reference that has surfaced appears in an 1869 letter from Harry Wright of the Cincinnati Red Stockings to a friend:”The spectators all arise between halves of the seventh inning, extend their legs and arms and sometimes walk about. In so doing they enjoy the relief afforded by relaxation from a long posture upon hard benches.”
The most popular story of its origin is also the most colorful. It was created in 1910 when President William Howard Taft, on a visit to Pittsburgh, went to a baseball game and stood up to stretch
in the seventh inning. The crowd, thinking the Chief Executive was about to leave, stood up out of respect for the office.
The term itself can be traced back no further than 1920.
Reference-The New Dickson Baseball Dictionary,Paul Dickson
Also read Origin of term ‘Bullpen’