Magnus the Good and Sweyn II Estridsson seems to have fought each other for a long time. Why would the former give Denmark to the later ?
I'd say because he didn't have any heirs, only a bastard daughter already married, so there was no chance of setting up a co-ruler besides Harald for Norway, but Harald was co-king only for Norway, and not Denmark, and the two didn't have such a good relationship, as the reason why Harald was named Co-king of norway was so that he stopped raiding Norway while pursuing his claim to the throne accompanied by Sweyn who wasn't included in this later arrangement, leaving him betrayed by Harald, and with a Claim to Magnus' crown. Owing to the fact that Magnus and Harald bickered over everything, he decided to give the Danish crown to Sweyn, to simply put, fuck with Harald, who tried over and over again to take Denmark from Sweyn, unsuccessfully.
edit: typo