Port 502 isn't in the actual Modbus/TCP protocol messages. However, it is what TCP/IP uses to tell the operating system (or firmware) at the destination which program to deliver the message to when it gets there (UDP/IP works exactly the same way). TCP/IP is like the address of an apartment building, while the port number is like the number of the apartment in the building.
You can change the port number, provided both ends of the connection know about the change, and provided the software allows you to make the change. When Modbus was made to run on Ethernet, someone thought that 502 was a good convention for everyone to use so that you wouldn't have to set the port number as well as the IP number. A lot of software however may simply have hard coded that number in instead of making it an option that you can change.
If you are running the Modbus/TCP server on a PC, you may be able to re-map the port numbers so that it looks like one number from the outside while the program itself is running a different port number. I can tell you how to do this with a Linux OS. I don't know if MS Windows (if you are using that) has that capability however.
The specification states port 502 for connectivity between Modbus/TCP clients and server.
If you deviate from the specification by changing port number, all clients would need the capability of using an alternate port number.
Sometimes, it's necessary to run multiple servers from a single IP Address, so we provide a provision in all of our Modbus/TCP client and server products to change port number.
For proprietary applications, there should be no problem using an alternate port number as long as clients and server have that provision. If you are building a commercial product, you should default to 502 but might want to build in the capability to specify an alternate port.