By serendipity; by blind luck in combination with wisdom; by fortunate accident.
Unexpectedly or by surprise.
Without plan or forethought.
Inadvertently.