We present a detailed analysis of the effect of heat treatments on the microstructure, magnetization, and transport properties of MgB2 wires produced by the powder-in-tube method. We have used commercial MgB2 powder with 5 at. % Mg powder added as an additional source of magnesium and stainless steel as sheath material. We measure the dc transport critical current that can be increased or decreased by more than one order of magnitude as compared with the as-drawn wire, depending on the annealing parameters. We correlate the changes in the critical current with changes in the microstructure, as determined from scanning and transmission electron microscopy analysis. We show through magnetization measurements of short annealed wires that inappropriate annealing conditions result in a deterioration of the connectivity due to the loss of Mg and in inhomogeneous weak-link limited current flow, rendering the critical state model inapplicable. We discuss the optimization of the annealing conditions that strongly improve the connectivity by eliminating most of the microcracks present in the unannealed wires, where excess Mg promotes the recrystallization. The loss of Mg during the heat treatment may be precluded by annealing long wire lengths with a high heating rate. © 2003 American Institute of Physics.