I must forewarn you, these are totally untested ideas. Just loud thinking.

HARDWARE SETUP:

You will need slaves that have a full duplex RS485 channel. What I mean is that the slaves should be able to see their own transmissions. This way we will have CSMA-CD (collision detection)

FIRMWARE

The master sends a broadcast (slave address 0) to set ID to 1 that is 'seen' by all the slaves. After a random delay, each slave will try to respond to the broadcast. Each slaves' transmit routine is written such that it checks that its own transmission is received back without data being corrupted(data collision) If there is a collision, the slave 'backs-off'.

In this process, one of the slaves will be detected and can take the set ID for itself. The master now increments the set ID.

You then repeat the process for the rest of the slaves. In case there are indeterminate number of slaves at the start, some kind of timing scheme will be needed to detect that condition(no responses from slaves) and terminate the discovery process.