There are two factors involved. First, the iron filings turns into tiny magnets because their domains align in the field. Second, if the filing is not parallel to the external magnetic field lines, a net torque acts on it and rotates it until it aligns along the field lines, just like a tiny compass needle.
Work Step by Step
This is shown in Figure 24.3.