My question is: How to understand what is happening? Is the Mac detecting the properties of the two cables somehow?
The Mac is detecting the properties of the cables.
USB-C cables will have a resistor network or small IC to indicate to connected devices the capabilities of the cable. A 60 watt cable will have a resistor network or C to tell connected devices it is capable of up to 20 volts and up to 3 amps. A 100 watt cable will have an IC to indicate up to 20 volts and up to 5 amps.
Is there a setting on one or both devices to enable me to set the flow
I'm not aware of any setting to specify the flow of power. By knowing some of the rules on USB there's ways to at least indicate preferences on the flow of power.
Is it just a fluke and might flow in the other direction next time? Is
there a specification somewhere for how this works/should work?
This does sound like something of a fluke since the phone should not take precedence over a power brick on supplying power to the MacBook. There are specifications on how this works, Apple has resources on their website laying out the rules their products follow on charging and USB has published specifications on how certified products should function. I have to wonder if your cable and phone are following the spec.
Perhaps the first and most important rule to know is that Apple devices will pull power from only one source at a time, and will always choose to draw power from the device advertising it can provide the most power. I'm not sure what the Apple rule is on if there are two power supplies of equal power connected, perhaps it draws power from the first device connected or there's some randomness to it.
My best guess on what is happening is somehow the phone is reporting itself as capable of providing more power than the power brick. While that is unlikely it is possible. I have to wonder if the Android phone is somehow lying about its ability to supply power, the non-Apple cable is somehow interfering with the power negotiation, or both.
One possible way to specify the flow of power is to do what you happened upon in your experimentation, use the cheap cable to connect the MacBook to the power brick and the Apple cable to connect to the phone. My guess though is that the direction of power flipped not because you moved the cable but because you disconnected everything and then reconnected.
Another rule to keep in mind is that when connecting two USB-C devices, and both are capable of supplying or sinking power, then the flow of power is picked at random. But to reverse the flow of power all one needs to do is disconnect the cable for a short time and then reconnect. There's a short memory on the direction of power flow, and it is the process of disconnecting and reconnecting that changes the flow of power. It is this behavior that can give the impression of moving cables about or flipping a cable around to direct the flow of power but it is just the disconnect and reconnect that is flipping the power flow.
As far as I know the USB-C charging cable that Apple includes with their devices are USB 2.0 20 volt 5 amp versions. Maybe that has changed with the new Apple products and the update to the USB-PD spec but last I checked all Apple "charge only" USB-C cables are rated for 100 watts. I use "charge only" in quotes because that is what Apple calls them in spite of being fully capable of USB 2.0 data, my guess is they use that nomenclature to discourage tech support calls on why data transfer is slow. If what I suspect is true then it is a bit odd that the MacBook would prefer to draw power from the phone than the power brick. If both cables are rated for 100 watts, and meet the USB spec, then the MacBook thinks the phone can supply more power. It is possible that the phone can provide more power than the power brick but unlikely unless the power brick is one of the 18 or 20 watt bricks.
Here is how I solved this problem...
USB-C can supply power in both directions but USB-A cannot. By pairing a USB-C to USB-A adapter with a USB-A to USB-C cable I can connect two USB-C devices together and dictate which device supplies power by which device has the adapter connected. This will limit power to what USB-A can handle, which is 12 watts, but given the situation I suspect this is sufficient.
Since a power brick will only supply power, not sink it, there's no directing power flow with a USB-A connection in the middle. It will supply power or not. It will limit power flow to 5 volts at 2.4 amps, and some people do this as they believe a slower charge will extend battery life. I have my doubts that is all that effective, and with better battery management software in phones and such there's likely no longer any need for such hacks.
Perhaps I digress too much with unnecessary detail, what the point is that the power flow is limited by the weakest link. By intentionally putting in a weak link you can direct how the power flows with your USB-C devices. I got a USB-C to USB-A adapter for $8, and a USB-A to USB-C cable for another $8, so for $16 I know I'm not going to have power flow from my iPad into my MacBook while my MacBook and iPad is plugged in to charge. I don't know what the adapters and cables sell for now but the point is that the fix is something relatively inexpensive and readily available.