Because the alternative was letting Tony Stark die. My theory is that Strange saw that the one winning scenario involves Tony heavily, and so despite what he’d said earlier, he needed to trade the Time Stone for Stark’s life. Hence the “It was the only way” line.