Being from Canada, I couldn't believe that this was possible when I first came down here because it seems to defy belief that any bank would take on the risk to extend a loan at such low-interest over that kind of time frame. It turns out that it's possible because the banks aren't taking any risk; instead it's all outsourced to US taxpayers ;)
Banks have been selling loans for ages - when I bought my first house in the '90s the bank which originated the loan sold it to a life insurance company four days after escrow closed. After a few years that insurance company sold it to someone else.
Life insurance is a good fit for mortgages. Insurance companies need a safe investment to pay out when people die, and usually the payout isn't inflation adjusted.
Besides, if you have to invest money for the long term but can't (by statute or temperament) handle much risk, what are you going to invest in? Twenty year T-Bills are at 2.30%.
FYI, T-bills have maturities of one year or less, T-notes two to ten years, and T-bonds 10 to 30 years. The term "Treasuries" helpfully covers all three.
The US does a lot of things strangely, this is but one of them :)