I broke my phone while away in Spain and had to purchase a new one there.
First, if you are already using your phone, you are mostly likely not eligible for a tax refund, as it is reserved to goods exported from EU without being used in the EU. The border post is allowed to request you to demonstrate your goods under unused condition when processing your tax refund forms.
Now legally you are still required to pay tax to Switzerland, It is your decision if you want to follow the law or risk punishment if you are checked ;). If you do not declare or request a tax refund, the chance of Swiss customs verifying the origin of your phone is essentially nil.
The complexity is because there are two borders to cross.
In the context of your particular situation, there is only one border to cross, that is, the customs border between the EU Customs Union (including all EU countries, Monaco and Akrotiri and Dhekelia) and the Swiss customs territory (Switzerland, Liechtenstein and Büsingen).
There is no customs control between Spain and France, as they are both EU countries.
What to do at each border post regarding customs?
Routine passport control for persons is abolished within the Schengen area (incl. Spain, France and Switzerland) and between two EU countries (Spain and France) there is no customs control. If you are taking the train back home, you will most likely not see any border control or customs staff on your journey. It is still possible for there to be on-board random passport or customs inspections as your cross national and customs borders, but these are very basic controls or they are looking for particular persons (sometimes even showing the cover of your passport can suffice).
May I have to pay duty twice?
No. You only have to pay Swiss customs. There is no customs duty for electronics between EU and Switzerland, however, you must pay Swiss valued-added tax (currently 7.7%; planned increase to 8.1% in 2024) on the pre-tax price indicated on your EU invoice.
Should I claim my tax refund while crossing between Spain and France or between France and Switzerland?
You must claim your tax refund when you leave from EU, i.e. between France and Switzerland. In practice, this can be difficult for train journeys. There is a staffed French customs at the Geneva central train station, but the working hours are not regular (but it should be staffed whenever there is a TGV arriving at or departing from Geneva within a two-hour window). You may also go to another French (or German or Italian or Austrian) border post during the working hours (to be checked online or by local experience) to certify the export from EU with your unused goods within three months (check the time written on your tax refund form to be sure).