You don't "have to" stay overnight, but hotels are significantly cheaper in Macau than Hong Kong that it makes sense to stay there overnight if you have the time.
But if you do want to stay in Macau but don't like checking out and then in again at a HK hotel, then either going to Macau first or last will work. There are ferries that go from airside of HKG to Macau, so you don't have to go through HK immigration at all.