Was this a special design built for NASA, or perhaps a prototype that nobody wanted for commercial use?
NASA has lots of cool aircraft. They get them sometimes when they get to the end of their life/retired, sometimes they are custom built trainers, and sometimes they are used to test research projects. You can find the full history of that particular 737 here
NASA’s Boeing 737-100 Transport Systems Research Vehicle was the prototype 737, acquired by the Langley Research Center in 1974 to conduct research into advanced transport aircraft technologies.
The Boeing 737 was Langley's number one choice, however, since it had a slightly wider fuselage and an advanced, high-lift flap system. Money however, was extremely tight. The market value of a used 737 in 1972 was about $3.5 million, but the Boeing Company had one particular 737 that its sales people said they might be willing to sell for substantially less."
Boeing had designated the prototype as PA-099: PA for Lufthansa, and 099 as the last one in a block of 100 aircraft numbers Boeing had reserved for the airline. The prototype was never sold, however, because it was only certified for experimental use. With all the holes, wiring and other modifications that were made to the airplane for certification tests, bringing it up to the standards of a commercial transport airplane would have been too expensive. Boeing used the airplane for a few additional flight tests and then simply set it aside. '
In short, it was cheap, couldn't be used for commercial flight, and just what NASA needed. Chapter 2 in the above linked document covers the history in more depth and explains more of the nuances of the deal. To keep from copying the whole text I'll end it here.
The design was originally built for the military and Langley air-force base as Dave mentioned. It was then modified and given to NASA as the requested vehicle they needed. The ease of transfer was primarily due to NASA and Langley have been deeply connected, even since NASA was first formed. I cannot reveal all the details, but here is an example of someone who was connected to both Langley and the Apollo 11 astronauts: https://www.findagrave.com/memorial/138018057/howard-mac-lane
There were many Langley engineers and staff that worked on expediting approval and modifying the 737 for NASA to use.