Like the others said, you need to use the the stuff Colress gives you on the mountain on your way to the Pokemon league for the first time. At some point in the mountain, you will encounter Necrozma in a crater and appears to be injured. If you interact with it, it will ask if you wanna share your Z power ring with it, or something along that line. Do that and a battle will start against Necrozma and you will be able to catch it this time.
After catching it, Colress appears and gives you some stuff which will fuse Necrozma with either Lunala or Solgaleo. To find either of those two, you need to go to the bridge you went to where you first encountered Lillie and Nebby at the start of the game. Lillie will ask you to take care of Nebby so catch it. Don't worry, if you kill it, it will just instantly respawn. Use the device Colress gave you on Necrozma and it will fuse with the either Lunala or Solgaleo, depending on who you choose. Use the device again to undo the fusion.
To change into Ultra Necrozma, just tap the ultra burst button when Necrozma is in dawn wings or dusk mane form. You can only do this during a battle and the burst button can be found when you are selecting a move. It is where the z crystal button is.