Often, mainframes came with source code. It was sold with the hardware, was very tied to the hardware, there were no mainframe clones on sale, and very few could build one (and have the space to host and run it). Licensee were well known, and giving access to code could wasn't a big issue, and customer may have needed to personalize it. Also, big money was made selling the hardware, not the software.
Illegal copies of software and IP became a much bigger issue later, with the advent of minis and personal PCs, ISV (software only companies), more competition, and off-the-shelf software. Now money was made selling software, not the hardware.
It's no surprise the FOSS movement was born in universities among people used to have access to the OS and application code on mainframes.