From Wikipedia:
They are protandric, which means that during their first year they spawn as males (releasing sperm into the water). As they grow larger over the next two or three years and develop greater energy reserves, they release eggs, as females. An increase in water temperature prompts a few initial oysters to spawn. This triggers a spawning 'chain reaction', which clouds the water with millions of eggs and sperm. The eggs become fertilized in the water and develop into larvae, which eventually find suitable sites on which to settle, such as another oyster's shell.