UBS Group AG agreed to buy Credit Suisse Group AG in a historic, government-brokered deal aimed at containing a crisis of confidence that had started to spread across global financial markets.